简体   繁体   中英

Use same RecyclerView.Adapter for different RecyclerView?

Is it possible somehow to use same RecyclerView.Adapter for different RecyclerView ? First problem is getItemCount method. How to know for which RecyclerView need to return count value?

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? 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 ). 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() ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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