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

Posted zsbenn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android基础—— 自定义RecyclerView内部控件的监听器相关的知识,希望对你有一定的参考价值。

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内部控件的监听器的主要内容,如果未能解决你的问题,请参考以下文章

Android自定义View基础

Android中自定义ViewViewGroup理论基础详解

Android自定义控件系列之基础篇

Android 自定义Lint 基础教程

Android基础篇 自定义广播

Android (基础自定义组件)viewpagertap滑动器