RecyclerView使用上下文菜单

效果图

效果图
  1. 布局文件中只包含RecyclerView
  2. MainActivity中代码如下:
public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerview;

    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerview = findViewById(R.id.recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        recyclerview.setAdapter(mAdapter = new MyAdapter());
        recyclerview.addItemDecoration(new DividerItemDecoration(this, OrientationHelper.VERTICAL));
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        mAdapter.removeItem(mAdapter.getPosition());
        return super.onContextItemSelected(item);
    }
}
  1. Adapter代码
public class MyAdapter extends RecyclerView.Adapter {

    private List mData = new ArrayList<>();

    private int mPosition = -1;

    public int getPosition() {
        return mPosition;
    }

    public void setPosition(int position) {
        this.mPosition = position;
    }

    public void removeItem(int position) {
        mData.remove(position);
        notifyDataSetChanged();
    }

    public MyAdapter() {
        String data;
        for (int i = 0; i < 100; i++) {
            data = "item_" + i;
            mData.add(data);
        }
    }

    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        TextView tv = new TextView(parent.getContext());
        tv.setTextSize(20);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-1, -2);
        tv.setLayoutParams(lp);
        return new MyAdapter.MyViewHolder(tv);
    }

    @Override
    public void onBindViewHolder(final MyAdapter.MyViewHolder holder, final int position) {
        holder.tv.setText(mData.get(position));
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mPosition = holder.getAdapterPosition();
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {

        TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView;
            itemView.setOnCreateContextMenuListener(this);
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            menu.add(0, 0, 0, "删除");
        }
    }
}

注意点:
: 1.在ViewHolder中添加itemView.setOnCreateContextMenuListener(this);
: 2.在ViewHolder中重写onCreateContextMenu添加菜单
: 3.在adapter中定义一个属性mPosition记录当前长按点击的位置
: 4.在onBindViewHolder中为itemView设置onLongClick事件
: holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { mPosition = holder.getAdapterPosition(); return false; } });
: 5.在Activity中重写onContextItemSelected方法实现上下文点击的操作。

你可能感兴趣的:(RecyclerView使用上下文菜单)