简体   繁体   English

对不同的RecyclerView使用相同的RecyclerView.Adapter吗?

[英]Use same RecyclerView.Adapter for different RecyclerView?

Is it possible somehow to use same RecyclerView.Adapter for different RecyclerView ? 是否有可能以某种方式使用相同RecyclerView.Adapter针对不同RecyclerView First problem is getItemCount method. 第一个问题是getItemCount方法。 How to know for which RecyclerView need to return count value? 如何知道哪个RecyclerView需要返回计数值?

Create a new instance of RecyclerView.Adapter, it seems like you're confused about it being a class that holds static fields, or did you really want it to have static fields? 创建一个RecyclerView.Adapter的新实例,您似乎对它是一个包含静态字段的类感到困惑,还是您真的希望它具有静态字段? In that case I think that is a bad design decision. 在那种情况下,我认为这是一个错误的设计决定。

The adapter is only a way of saying to the RecyclerView how to create a view holder ( onCreateViewHolder ) and how to populate it ( onBindViewHolder ). 适配器只是对RecyclerView说如何创建视图持有者( onCreateViewHolder )以及如何填充它( onBindViewHolder )的一种方式。 The data used to fill the view's children may just be different for each adapter. 每个适配器用于填充视图子级的数据可能有所不同。

In order to use different data in each adapter create your own RecyclerView.Adapter 's subclass and pass the right data to its constructor (the data which will be used by the adapter itself, also by its getItemCount() ). 为了在每个适配器中使用不同的数据,请创建您自己的RecyclerView.Adapter的子类,然后将正确的数据传递给其构造函数(该适配器本身以及getItemCount()将使用的数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM