recListview中长按删除及点击事件

在当前adapter中声明接口

  //点击事件
    public interface click{
        void click(View view,int position);
    }
    //长按事件
    public interface longclick{
        void longclick(View view,int position);
    }
    //声明接口
    public click click;

    public longclick longclick;

    //设置监听方法
    public void setClick(MlssAdapter.click click) {
        this.click = click;
    }

    public void setLongclick(MlssAdapter.longclick longclick) {
        this.longclick = longclick;
    }

在onBindViewHolder方法中进行监听

---------------------------------------点击事件----------------------------------------
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int layoutPosition = viewHolder.getLayoutPosition();
                View itemView = viewHolder.itemView;
                if(click!=null){
                    click.click(itemView,layoutPosition);
                }
            }
        });
-------------------------------------------长按事件---------------------------------------
        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                int layoutPosition = viewHolder.getLayoutPosition();
                View itemView = viewHolder.itemView;
                if(longclick!=null){
                    longclick.longclick(itemView,layoutPosition);
                }
                return true;
            }
        });

在解析数据配置适配器以后写入方法

//点击长按事件
                mlssAdapter.setClick(new MlssAdapter.click() {
                    @Override
                    public void click(View view, int position) {
                        Toast.makeText(getActivity(),"点击",Toast.LENGTH_LONG).show();
                    }
                });

                mlssAdapter.setLongclick(new MlssAdapter.longclick() {
                    @Override
                    public void longclick(View view, int position) {
                        mlsslist.remove(position);
                        mlssAdapter.notifyItemRangeRemoved(position,1);
                        Toast.makeText(getActivity(),"删除成功",Toast.LENGTH_LONG).show();
                    }
                });
展开阅读全文

没有更多推荐了,返回首页