[英]Where should I place setOnClickListener in a RecyclerView Adapter
In tutorials on internet where they setOnClickListener in Adapter of RecyclerView they define it in two ways : either inside ViewHolder or inside BindViewHolder. 在互联网上的教程中,他们在RecyclerView的Adapter中设置了OnClickListener,它们以两种方式定义它:在ViewHolder内部或在BindViewHolder内部。
My Question is which one is a better approach, Please recommend any another approach if available 我的问题是哪一个是更好的方法,请推荐任何其他方法(如果有的话)
1) inside ViewHolder: 1)在ViewHolder中:
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
tvSrc = (TextView) itemView.findViewById(R.id.tvSrc);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "inside viewholder position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
}
2) inside BindViewHolder 2)在BindViewHolder里面
public void onBindViewHolder(DisplayTrainsAdapter.ViewHolder viewHolder, final int position) {
viewHolder.tvSrc.setText(mDataset.get(position).strSrc);
viewHolder.tvSrc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "position = " + getItemId(position), Toast.LENGTH_SHORT).show();
}
});
}
Your number 1 solution is the best as you proposed since that assignment will not be called in the Binding at each invalidate triggered by notify..() methods. 你提出的1号解决方案是最好的解决方案,因为在notify ..()方法触发的每个invalidate中,不会在Binding中调用该赋值。 I know also others solutions but you need to implement android.view.GestureDetector in your activity. 我也知道其他解决方案但你需要在你的活动中实现android.view.GestureDetector。
If you want others improvements on the adapter, take a look at mine FlexibleAdapter https://github.com/davideas/FlexibleAdapter and feel free to implement in your project. 如果您希望其他人对适配器进行改进,请查看我的FlexibleAdapter https://github.com/davideas/FlexibleAdapter ,随时在您的项目中实施。
Both options have their pros and cons. 两种选择都有其优点和缺点。
For example, if a Button is clicked and you want to change the text of your button, then you should probably use the option where you setup the onClick listener in the ViewHolder. 例如,如果单击一个按钮并且您想要更改按钮的文本,那么您应该使用在ViewHolder中设置onClick侦听器的选项。 Apart from this reason, it also makes your code look cleaner. 除此之外,它还使您的代码看起来更干净。
However, if say, when the Button is clicked, you want to change the text of a TextView in the same index/position as the button that was clicked, you will need to use the option where you setup the onClick listener in the onBindViewHolder method. 但是,如果单击按钮,您想要在与单击的按钮相同的索引/位置更改TextView的文本,则需要使用在onBindViewHolder方法中设置onClick侦听器的选项。
IMHO: I like number 1. 恕我直言:我喜欢1号。
Since your calling into new ViewHolder(View)
your really setting your onClickListener before actually displaying your content. 由于您调用了new ViewHolder(View)
在实际显示内容之前,您真正设置了new ViewHolder(View)
。 This is nice because by the time onBindView is called, your onClickListener has already been set on your view. 这很好,因为在调用onBindView时,你的onClickListener已经在你的视图上设置了。
I think its also cleaner code to do this in your constructor ViewHolder(View)
我认为在构造函数ViewHolder(View)
执行此操作也更干净
In ViewHolder()
i guess, since you define what view
is holded and what it has inside and other functions. 在ViewHolder()
我想,因为你定义了什么view
被保持以及它内部和其他功能。
But onBindViewHolder()
you say that view
, which is defined in ViewHolder
, will have this text, this image... 但是onBindViewHolder()
你说在ViewHolder
定义的view
会有这个文本,这个图像......
You should always check if getAdapterPosition is >= 0 because it could be -1 (NO_POSITION) in rare cases that can lead to a crash in your app. 您应该始终检查getAdapterPosition是否> = 0,因为它可能是-1(NO_POSITION),在极少数情况下会导致应用程序崩溃。 https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder#getadapterposition https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder#getadapterposition
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.