简体   繁体   English

在Adapter或ViewHolder中注册浮动上下文菜单单击侦听器是否更好? [ANDROID]

[英]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 我以这种方式在ActivityFragment中使用它

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.

相关问题 如何通过单击打开上下文浮动菜单? - How can i open a contextual floating menu upon a single click? 使用onLongPress [ANDROID]上的GestureDetector打开浮动的上下文菜单 - Opening a floating contextual menu by using GestureDetector onLongPress [ANDROID] 如何在 ViewHolder RecyclerView 适配器中单击时关闭底部表? - How to dismiss Bottomsheet on Click in ViewHolder RecyclerView Adapter? 单击菜单后打开上下文菜单栏 - Open a contextual menu bar upon menu click Android:如何在自定义适配器中设置侦听器并获取图像按钮单击的位置 - Android : how to set listener and get position of imagebutton click in a custom adapter 适配器的图像加载器侦听器中ViewHolder的错误初始化导致崩溃 - Wrong initialisation of ViewHolder in image-loader listener in Adapter causes crash Android:在“项目”上单击“侦听器”以实现列表视图基本适配器实现 - Android: On Item Click Listener for List View Base Adapter implementation 如何进入listview适配器项目在android中单击侦听器片段? - How to go listview adapter item click listener to fragment in android? Android:使用ViewHolder,OnCheckedChangedListener和TextChangedListener的自定义适配器 - Android: Custom Adapter with ViewHolder, OnCheckedChangedListener and TextChangedListener 在Custom Listview Adapter android中使ViewHolder最终化 - making ViewHolder final in Custom Listview Adapter android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM