您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

RecyclerView实现长按弹出菜单

public class AddressListAdapter extends BaseQuickAdapter<AddressListResult.Address, AddressListAdapter.ViewHolder> {
    private int position;

    public AddressListAdapter(@Nullable List<AddressListResult.Address> data) {
        super(R.layout.item_addressshow, data);
    }

    public int getPosition() {
        return position;
    }

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

    @Override
    protected void convert(ViewHolder helper, AddressListResult.Address item) {
        helper.getTextViewName().setText(item.getConsignee());
        helper.getTextViewAddress().setText(item.getAddress());
        helper.getTextViewArea1().setText(item.getArea1());
        helper.getTextViewTelNumber().setText(item.getTel());
        helper.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int a = helper.getLayoutPosition();
                Log.d("debug", "長按時的position->>>>>>" + a);
                setPosition(a);//
                return false;
            }
        });
    }

    public class ViewHolder extends BaseViewHolder implements View.OnCreateContextMenuListener {
        private TextView textViewName;
        private TextView textViewAddress;
        private TextView textViewArea1;
        private TextView textViewTelNumber;

        public ViewHolder(View view) {
            super(view);
            textViewName = view.findViewById(R.id.address_name_tv);
            textViewAddress = view.findViewById(R.id.address_address_tv);
            textViewArea1 = view.findViewById(R.id.address_area1);
            textViewTelNumber = view.findViewById(R.id.address_telnumber);
            view.setOnCreateContextMenuListener(this);
        }

        public TextView getTextViewName() {
            return textViewName;
        }

        public void setTextViewName(TextView textViewName) {
            this.textViewName = textViewName;
        }

        public TextView getTextViewAddress() {
            return textViewAddress;
        }

        public void setTextViewAddress(TextView textViewAddress) {
            this.textViewAddress = textViewAddress;
        }

        public TextView getTextViewArea1() {
            return textViewArea1;
        }

        public void setTextViewArea1(TextView textViewArea1) {
            this.textViewArea1 = textViewArea1;
        }

        public TextView getTextViewTelNumber() {
            return textViewTelNumber;
        }

        public void setTextViewTelNumber(TextView textViewTelNumber) {
            this.textViewTelNumber = textViewTelNumber;
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            int groupID = 0;
            int order = 0;
            int[] itemID = {1, 2};
            for (int i = 0; i < itemID.length; i++) {
                switch (itemID[i]) {
                    case 1:
                        menu.add(groupID, itemID[i], order, "刪除");
                        break;
                    case 2:
                        menu.add(groupID, itemID[i], order, "修改");
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
在Activity中重写如下方法
@Override
public boolean onContextItemSelected(MenuItem item) {
    if (item.getTitle().equals("刪除")) {
        Log.d("debug", "删除按钮点击");
    }
    return super.onContextItemSelected(item);
}

分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进