[英]Is it better to register floating contextual menu click listener in Adapter or in ViewHolder? [ANDROID]
I'm planning to create a floating contextual menu for my app in a fragment after onLongClick and I also have a custom Adapter extending from RecyclerView.Adapter and a custom holder extending from RecyclerView.ViewHolder. 我计划在onLongClick之后的片段中为我的应用程序创建一个浮动的上下文菜单,并且我还有一个从RecyclerView.Adapter扩展的自定义适配器和一个从RecyclerView.ViewHolder扩展的自定义支架。 I found out there are 2 options to register :
我发现有2个选项可以注册:
Option 1 : Registering the listener inside onCreateViewHolder method by holder.itemView.setOnLongClickListener
and retrieving any ViewHolder instance variable I want by calling holder.getSomeVariable()
inside the event handler and set it to current instance variable so I can retrieve it from Activity/Fragment (because I can get reference of adapter from my fragment). 选项1 :通过
holder.itemView.setOnLongClickListener
在onCreateViewHolder方法中注册侦听holder.itemView.setOnLongClickListener
并通过在事件处理程序中调用holder.getSomeVariable()
检索我想要的任何ViewHolder实例变量,并将其设置为当前实例变量,以便从Activity / Fragment中检索它(因为我可以从片段中获取适配器的引用)。
The advantage I notice is that it allows me to declare the implementation onCreateContextMenu()
and onContextItemSelected()
method and override them in my Fragment/Activity, which allows the reusing of the existing provided method and get the ViewHolder
variables inside the onContextItemSelected()
. 我注意到的优点是,它允许我声明实现
onCreateContextMenu()
和onContextItemSelected()
方法,并在我的Fragment / Activity中覆盖它们,从而可以重用现有的提供的方法并在onContextItemSelected()
获取ViewHolder
变量。 I notice the downside is that we need to create extra setter and getter method in the adapter (because we cannot reference ViewHolder instance from Activity/Fragment). 我注意到缺点是我们需要在适配器中创建额外的setter和getter方法(因为我们无法从Activity / Fragment中引用ViewHolder实例)。
Option 2 : Registering the click listener inside ViewHolder. 选项2 :在ViewHolder中注册点击监听器。 According to
this website
,the advantage is it allows more explicit click listener (I also have other listeners registered in the ViewHolder class, so they're group together which makes the code structure looks better). 根据
this website
说法, this website
的优点是允许使用更明确的单击侦听器(我还在ViewHolder类中注册了其他侦听器,因此它们组合在一起,使代码结构看起来更好)。 However, I notice the downside from using this way is we cannot declare onCreateContextMenu()
and onContextItemSelected()
inside Activity/Fragment anymore because we cannot get the viewholder reference from Activity/Fragment, we can only get adapter reference. 但是,我注意到使用这种方式的缺点是我们不能再在Activity / Fragment中声明
onCreateContextMenu()
和onContextItemSelected()
了,因为我们无法从Activity / Fragment中获取viewholder引用,因此只能获取适配器引用。 As a result, the readability is a bit worse. 结果,可读性稍差。 (I have a contextual_menu.xml, so will need to do the inflation in the viewholder as well instead of in fragment/activity. Is inflation of XML in viewholder even a correct practice?)
(我有一个contextual_menu.xml,因此也需要在视图持有人中进行膨胀,而不是在片段/活动中进行膨胀。在视图持有人中进行XML膨胀甚至是正确的做法吗?)
From what I understand, in general scenario , it's better to register listener in ViewHolder (Option 2). 据我了解,一般情况下,最好在ViewHolder中注册侦听器(选项2)。 But in my context, which method (Option 1 or Option 2) should I apply?
但是在我的上下文中,我应该使用哪种方法(选项1或选项2)? Does the general scenario still apply (should I apply Option 2)?
通用方案是否仍然适用(我应该适用选项2)吗? Also are there any more advantages and disadvantages from those 2 listed options?
另外,这两个列出的选项还有其他优点和缺点吗?
I use it in Activity
or Fragment
that way 我以这种方式在
Activity
或Fragment
中使用它
1- Create interface
like that: 1-创建这样的
interface
:
public interface OnItemLongClickListener {
void onItemLongClick(int position);
}
EDIT: 编辑:
2- In your 2-在你的
Adapter 适配器
ViewHolder
class create a reference like that: ViewHolder
类创建类似的引用:
private OnItemLongClickListener listener;
3- In adapter create setter
method to initialize listener
which will call another setter
method in ViewHolder
class like that: 3-在适配器中,使用
ViewHolder
setter
方法初始化listener
,这将在ViewHolder
类中调用另一个setter
方法, ViewHolder
所示:
class Adapter extends RecyclerView.Adapter<YourViewHolder>{
.
.
.
.
public void setOnItemLongClickListener(OnItemLongClickListener listener){
viewHolderObject.setonItemLongClickListener(listener);
}
}
public class YourViewHolder extends RecyclerView.ViewHolder{
.
.
.
.
public void setOnItemLongClickListener(OnItemLongClickListener listener){
this.listener = listener;
}
}
4- In your ViewHolder in method onLongClick() do this: 4-在方法onLongClick()的ViewHolder中,执行以下操作:
@override
public boolean onLongClick(View v){
listener.onItemLongClick(getAdapterPosition());
}
5- In your Activity Do the contextual menu staff 5-在您的活动中执行上下文菜单人员
I hope this help you.! 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.