码迷,mamicode.com
首页 > 移动开发 > 详细

Android基础—— 自定义RecyclerView内部控件的监听器

时间:2020-05-26 01:21:09      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:定义   erp   klist   监听   length   puts   int   其他   lis   

RecyclerView内部控件的监听器要写在对应的Adapter中

1.在Adapter里写好要用的回调接口

public interface OnItemClickListener1{ // 进入商品详情监听
        public void onItemClick(int pos);
    }
    public interface OnItemClickListener2{ // 添加到购物车监听
        public void onItemClick(int pos);
    }
    OnItemClickListener1 onItemClickListener1;
    public void setOnItemClickListener1(OnItemClickListener1 onItemClickListener1) {
        this.onItemClickListener1 = onItemClickListener1;
    }
    OnItemClickListener2 onItemClickListener2;
    public void setOnItemClickListener2(OnItemClickListener2 onItemClickListener2) {
        this.onItemClickListener2 = onItemClickListener2;
    }

2.在构造ViewHolder时给对应的控件注册OnClick监听,在OnClick里面调用上面写好的接口

public ViewHolder(View view){
            super(view);
            cardView = (CardView)view;
            coffeeImage = (ImageView)view.findViewById(R.id.coffee_image);
            coffeeName = (TextView)view.findViewById(R.id.coffee_name);
            coffeePrice = (TextView)view.findViewById(R.id.coffee_price);
            addToCart = view.findViewById(R.id.add_to_cart);
            // 设置点击监听事件
            coffeeImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener1!=null)
                        onItemClickListener1.onItemClick(getAdapterPosition());
                }
            });
            addToCart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener2!=null)
                        onItemClickListener2.onItemClick(getAdapterPosition());
                }
            });
        }

3.外界调用时,就可以选择是否给这些控件注册我们自己定义的监听器,只要调用Adapter对象的setOnItemClickListener方法注册就行

例子为Adapter的构造函数里set,在其他地方同理

 public CoffeeAdapter(List<Coffee> list,Context context){
        mCoffeeList = list;
        mContext = context;
        setOnItemClickListener1(new OnItemClickListener1() {
            @Override //点击图片
            public void onItemClick(int pos) {
                Coffee coffee = list.get(pos);
                Intent intent = new Intent(mContext, CoffeeInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("coffee",coffee);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        setOnItemClickListener2(new OnItemClickListener2() {
            @Override //点击按钮添加到购物车
            public void onItemClick(int pos) {
                CartStorage.getInstance().addData(list.get(pos));
                QBadgeView qBadgeView = ((MainActivity)mContext).getqBadgeView();
                Log.d(TAG, "onItemClick: "+qBadgeView.getBadgeNumber());
                Toast.makeText(mContext, "添加成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

 

Android基础—— 自定义RecyclerView内部控件的监听器

标签:定义   erp   klist   监听   length   puts   int   其他   lis   

原文地址:https://www.cnblogs.com/zsben991126/p/12961961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!