[英]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.