Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

java - 从ViewHolder获取适配器实例(Getting an adapter instance from a ViewHolder)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe I'm not understanding well your pattern, but the reference you are looking for, it is already there.

(也许我不太了解您的模式,但是您正在寻找的参考资料已经存在。)

The fragmens knows the adapter reference, and the adapter knows your viewholder.

(fragmens知道适配器参考,而适配器知道您的视口。)

Use getAdapterPosition() to get that reference to your Object

(使用getAdapterPosition()获取对该对象的引用)

Suppose to have a ArrayList<String> named list in your RecyclerView.Adapter<ViewHolder> : this code, for example, shows a Toast in the Fragment with the value of the clicked String

(假设在您的RecyclerView.Adapter<ViewHolder>有一个名为ArrayList<String> 列表 :例如,此代码在Fragment中显示一个Toast,其中包含单击的String的值。)

public ViewHolder( View itemView) {

    super(itemView);

    itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String myValue = list.get( getAdapterPosition() ); // your object 
            Toast.makeText(activity, myValue, Toast.LENGTH_SHORT).show();
        }
    });

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...