简体   繁体   English

如何以及在哪里为两个RecyclerView实例设置OnClickListener侦听器

[英]How and where to set an OnClickListener listener for two RecyclerView instances

I have two RecyclerView in my Activity . 我的Activity有两个RecyclerView I set an OnClickListener for one of them and implement the onItemClick method. 我为其中之一设置了OnClickListener并实现了onItemClick方法。

If I want to set OnClickListener and implement onItemClick for the second RecyclerView , how do I achieve this? 如果我想设置OnClickListener并为第二个RecyclerView实现onItemClick ,如何实现呢?

The recommended way to add listeners to a RecyclerView is to make the ViewHolder implement the listener and then register the listener on the View that is passed to the ViewHolder constructor. 将侦听器添加到RecyclerView的推荐方法是使ViewHolder实现侦听器,然后在传递给ViewHolder构造函数的View上注册侦听器。 Example : 范例:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>    

    public class MyViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
        private TextView textView;

        public MyViewHolder(View view) {
            super(view);
            view.setOnClickListener(this);
            textView = (TextView)view.findViewById(R.id.tv_data);
        }

        @Override
        public void onClick(View v) {
            //do something on click using the position
             int adapterPosition = getAdapterPosition();
       }
   }  
}

If you have two RecyclerView classes, you need to set the listener in the second ViewHolder implementation in a similar fashion. 如果您有两个RecyclerView类,则需要以类似的方式在第二个ViewHolder实现中设置侦听器。

Note : While there are multiple ways in which one can register a listener for a RecyclerView , the above approach defines the ViewHolder implementaion as an inner class in the adapter class and also ensures that only classes that need to know about clicks contain the code for handling them. 注意:虽然可以通过多种方式为RecyclerView注册侦听器,但是上述方法将ViewHolder实现定义为适配器类中的内部类,并且还确保只有需要了解点击的类才能包含用于处理的代码。他们。

You can set click listner on both of the RecyclerView by RecyclerviewAdapter class. 您可以通过RecyclerviewAdapter类在两个RecyclerView上设置单击列表器。

Inside onBindViewHolder() of Adapter Class. 在Adapter类的onBindViewHolder()内部。 (Same code for both of the RecyclerView Adapter class) (两个RecyclerView Adapter类的代码相同)

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    LoadDataResult listPotn = list.get(position);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Call intent or call method of Activity from here.

            Context context = v.getContext();
            Intent intent = new Intent(context , Excercise.class);
            context.startActivity(intent);
        }
    });
}

Or, 要么,

public class ViewHolder extends RecyclerView.ViewHolder {
TextView product_name;

ViewHolder(View itemView) {
    super(itemView);
    product_name = (TextView) itemView.findViewById(R.id.product_name);
    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int itemPosition = getLayoutPosition();
            Toast.makeText(getApplicationContext(), itemPosition + ":" + String.valueOf(product_name.getText()), Toast.LENGTH_SHORT).show();
        }
    });
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM