仿QQ左滑删除SwipeMenuRecyclerView

compile 'com.yanzhenjie:recyclerview-swipe:1.1.4'

https://github.com/yanzhenjie/SwipeRecyclerView

1,setAdapter  一定要放在最后

 mHistoryAdapter = new TodayAdapter(getActivity(),data);

        mSwipeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mSwipeRecyclerView.addItemDecoration(new DefaultItemDecoration(Color.RED));

        //  设置侧滑
        // 设置监听器。
        mSwipeRecyclerView.setSwipeMenuCreator(mSwipeMenuCreator);
        mSwipeRecyclerView.setSwipeMenuItemClickListener(mMenuItemClickListener);

        mSwipeRecyclerView.setAdapter(mHistoryAdapter);


2,

  /**
     * 创建菜单
     */
    private SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int viewType) {
            int width = getResources().getDimensionPixelSize(R.dimen.dp_70);

            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity())
                    .setBackground(R.drawable.selector_purple)
                    .setImage(R.mipmap.ic_action_close)
                    .setWidth(width)
                    .setHeight(height);
            // 各种文字和图标属性设置。
            rightMenu.addMenuItem(deleteItem); // 在Item右侧添加一个菜单。

            SwipeMenuItem completeItem = new SwipeMenuItem(getActivity())
                    .setBackground(R.drawable.swipe_menu_shap)
                    .setText("完成")
                    .setTextColor(Color.WHITE)
                    .setWidth(width)
                    .setHeight(height);
            rightMenu.addMenuItem(completeItem); // 添加菜单到右侧。

            SwipeMenuItem modifyItem = new SwipeMenuItem(getActivity())
                    .setBackground(R.drawable.swipe_menu_shap)
                    .setText("修改")
                    .setTextColor(Color.WHITE)
                    .setWidth(width)
                    .setHeight(height);
            rightMenu.addMenuItem(modifyItem); // 添加菜单到右侧。

            SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity())
                    .setBackground(R.drawable.swipe_menu_shap)
                    .setText("删除")
                    .setTextColor(Color.WHITE)
                    .setWidth(width)
                    .setHeight(height);
            rightMenu.addMenuItem(deleteItem); // 添加菜单到右侧。

        }
    };
    /**
     * 菜单点击监听。
     */
    private SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
        @Override
        public void onItemClick(SwipeMenuBridge menuBridge) {
            // 任何操作必须先关闭菜单,否则可能出现Item菜单打开状态错乱。
            menuBridge.closeMenu();
            toast("history");

            int direction = menuBridge.getDirection(); // 左侧还是右侧菜单。
            int adapterPosition = menuBridge.getAdapterPosition(); // RecyclerView的Item的position。
            int menuPosition = menuBridge.getPosition(); // 菜单在RecyclerView的Item中的Position。

            switch (menuPosition){
                case 0 :
                    //  删除

              
                    break;
            }
        }
    };

3, adapter

package com.vily.noteplatform.adapt;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.vily.noteplatform.R;

/**
 * description :
 * Author : Vily
 * Date : 2018/02/06
 * Time : 15:25
 */

public class HistoryAdapter extends RecyclerView.Adapter {

    static final int VIEW_TYPE_NON_STICKY = R.layout.item_menu_main;

    private Context mContext;
    private LayoutInflater mInflater;


    public HistoryAdapter(Context context) {
        this.mContext=context;
        mInflater=LayoutInflater.from(context);

    }

    @Override
    public HistoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.history_item, parent, false);
        return new HistoryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(HistoryViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return 3;
    }


    class HistoryViewHolder extends RecyclerView.ViewHolder{

        public HistoryViewHolder(View itemView) {
            super(itemView);
        }
    }


}


你可能感兴趣的:(安卓)