RecyclerView长按删除

喜欢而已 提交于 2020-01-04 03:02:53

Adapter中添加

public interface LongClickLisenter {
    void LongClickLisenter(int position);
}

private LongClickLisenter longClickLisenter;

public void setLongClickLisenter(LongClickLisenter longClickLisenter) {
    this.longClickLisenter = longClickLisenter;
}


public void del(int i) {
    result.remove(i);
    notifyDataSetChanged();
}

 适配器onCreateViewHolder中添加

 view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            int layoutPosition = myViewHandler.getLayoutPosition();
            if (longClickLisenter != null) {
                longClickLisenter.LongClickLisenter(layoutPosition);
            }
            return false;
        }
    });

Activity

adapter.setLongClickLisenter(new MyAdapter.LongClickLisenter() {
        @Override
        public void LongClickLisenter(int position) {
            adapter.del(position);
            Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show();
        }
    });

 

 

 

 

 

 

 

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!