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();
}
});
来源:CSDN
作者:书中有颜如玉
链接:https://blog.csdn.net/weixin_37438128/article/details/103808980