RecyclerView的长按点击删除

**

先在Adapter适配器里定义接口回调

**

  public interface OnremoveListnner{
        void  ondelect(int i);
    }
    private OnremoveListnner onremoveListnner;

    public void setOnremoveListnner(OnremoveListnner onremoveListnner) {
        this.onremoveListnner = onremoveListnner;
    }

**

先在onBindViewHolder方法里边

**

  viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                if (onremoveListnner!=null){
                    onremoveListnner.ondelect(i);
                }
                return true;
            }
        });

**

第三步在Activity里调用接口

**

//实例化适配器
   final RecyclerView_Adapter recyclerView_adapter = new RecyclerView_Adapter(getContext(),jsonArray);
        recyclerView.setAdapter(recyclerView_adapter);

//调用适配器里的方法
        recyclerView_adapter.setOnremoveListnner(new RecyclerView_Adapter.OnremoveListnner() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void ondelect(int i) {
                jsonArray.remove(i);
                recyclerView_adapter.notifyDataSetChanged();
              Toast.makeText(getContext(),""+i,Toast.LENGTH_SHORT).show();
            }
        });

你可能感兴趣的:(RecyclerView的长按点击删除)