I'm trying to create a contextual floating menu with the information (position,id,etc) of the selected menu to be accessible inside the overridden method onContextItemSelected
in my Fragment.
(我正在尝试创建一个上下文浮动菜单,其中包含所选菜单的信息(位置,id等),以便可以在我的Fragment中的onContextItemSelected
重写方法内访问。)
So I have a custom adapter extending from RecyclerView.Adapter<ViewHolder>
and a custom ViewHolder
class in separate file.
(因此,我有一个从RecyclerView.Adapter<ViewHolder>
扩展的自定义适配器,并且在单独的文件中有一个自定义ViewHolder
类。)
I registered my click listener and provided its handler inside the custom ViewHolder
class. (我注册了我的点击监听器,并在自定义ViewHolder
类中提供了它的处理程序。)
Since I need to send the ViewHolder
information back to my Fragment when the event is fired, I need to have a reference to my Adapter because my fragment has the adapter object reference
but not viewholder object reference
.
(因为触发事件时需要将ViewHolder
信息发送回我的Fragment,所以我需要对Adapter进行引用,因为我的片段具有adapter object reference
但没有viewholder object reference
。)
So in instantiation stage of the custom ViewHolder
(because there is no method to get Adapter reference from ViewHolder
class), I pass the adapter reference to constructor of the custom ViewHolder
for the use in its handler so I can call this reference inside the ViewHolder
class to pass the information to Adapter
from ViewHolder
, and finally from Adapter
to Fragment
. (因此,在自定义ViewHolder
实例化阶段(因为没有从ViewHolder
类获取适配器引用的方法),我将适配器引用传递给自定义ViewHolder
构造函数以在其处理程序中使用,以便可以在ViewHolder
类内部调用此引用将信息从ViewHolder
传递到Adapter
,最后从Adapter
传递到Fragment
。)
My question is, is there a better practice or a general approach by still keeping the handler inside the ViewHolder
?
(我的问题是,是否仍将处理程序保留在ViewHolder
,是否有更好的实践或一般方法?)
In my case, I notice every ViewHolder instance will have to keep a reference to adapter, which consumes memory. (就我而言,我注意到每个ViewHolder实例都必须保留对适配器的引用,这会消耗内存。)
Is there any conflict that may arise in the future by doing it the way I listed above? (按照上面列出的方式进行操作,将来可能会发生任何冲突吗?)
ask by atjua translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…