Home > Enterprise >  can i have two recyclerView with one viewHolder and two adapters?
can i have two recyclerView with one viewHolder and two adapters?

Time:05-27

Hi i'm new of android and i have a question. if I have two recyclerviews, how many viewHolder and adapter should I have? can i have two recyclerView with one viewHolder and two adapters, or is it good practice to have one adapter and one viewHolder for each recyclerView?

CodePudding user response:

This entirely depends on your business requirements.

Basics -

Recyclerview - The UI part.

Adapter - Used to populate the recycler view using a few override methods.

ViewHolder - This is the unique UI items that are shown in your recycler view.

Scenario 1 -

Let's say you have a chat conversation screen in two activities. here both UI have the same use. so you must use the same adapter with its ViewHolder .

Scenario 2 -

You have a user list screen in two activities. but the difference is that you want to make one with infinite scroll and another with only a single page of data. now as you can see you have to show the same UI but the logic for loading the data changes. so I suggest you use two adapters (or some variation of it) with the same ViewHolder.

For your question -

if I have two recyclerviews, how many viewHolder and adapter should I have?

You can have only 1 adapter with one or multiple ViewHolder per Recyclerview at a time as per your requirements. However, you can swap adapters in case you want to reuse the Recyclerview in the UI. But to be safe use a single adapter.

can i have two recyclerView with one viewHolder and two adapters, or is it good practice to have one adapter and one viewHolder for each recyclerView?

Certainly possible. there is no hard and fast rule for this. but as you try to mix and match between these your business logic gets entangled.

The best practice would be to use a single Adapter per RecyclerView and the adapter will have its separate viewHolders (single or multiple). but don't reuse them unless your business requirements call for it.

CodePudding user response:

Whatever you like! An Adapter is a thing that takes data and manages displaying it in a list, a ViewHolder is the UI component that represents an item in that list. The RecyclerView basically says "hey I need item n", and it's the adapter's job to take a ViewHolder and set it up to display item n's data

The simplest way to organise it is to create an adapter for each list (each RecyclerView) and a ViewHolder designed to represent that list's items. So one set of components per list, designed specifically for that list. But if the two lists are basically the same - using the same data and representing it in the same way, and it makes sense to reuse stuff - then you can use the same components in both if you like

You just need to make sure they are the same, and you're not trying to generalise too much and going out of your way to make things that work "in multiple situations" when the simpler approach is to make the thing that does the specific job. That's programming in general though - knowing when to reuse stuff, and knowing when to "duplicate" it, that comes with experience. Reusing stuff is good, unless it complicates things and ties stuff together that really should be independent

btw if you have one adapter, you probably have one ViewHolder, because the adapter needs to be written to work with a particular type of ViewHolder (setting text on a TextView with a certain ID, etc). You could write it to handle two completely different ViewHolders, but then you're getting into the complexity thing - it's easier to just write an adapter that works with one type of item, unless you have good reason (like a single RecyclerView that contains more than one type of item, that need different ViewHolders with different layouts)

  • Related