Adapter Delegate (Favor Composition Over Inheritance in Multiple Layout RecyclerView Adapter)
Creating multiple item layouts in the list can make our RecyclerView
adapter
complex and hard to read, even difficult to maintain. Here, Adapter Delegate comes to solve these problems.
Found a New Problem
Inside the Sampingan app, several pages display a list with more than two ViewItems. At first, We didn’t see any problem with it until we read this article:
We believe that the adapter will become a big problem one day, and we need to fix it before that happens. Therefore, we started researching to solve this problem.
Let’s say we want to create this layout:
As we can see, the above layout contains several ViewItems
:
- Banner
- Promotion
And I’m adding several other views to make that layout more complex. Let’s say we have:
- Campaign
- Featured
If we’re still using the typical approach of creating the RecyclerView
, our adapter will look like this:
The above snippet looks good, but when we want to add more layouts to it, it will become more complex than it should be. And we have to add more logic to getItemViewType()
, onCreateViewHolder()
, and onBindViewHolder()
. That’s the problem that we should avoid.
Fixing the Problem
It appears that Hannes Dorfmann in his article has given great ways to solve this problem using a library named AdapterDelegate
But, we were still curious to find another approach to understand the solution better. And finally, we have found an interesting article that shows us how to create adapter delegates independently.
The idea is still the same: we need to simplify our adapter to maintain it quickly, even when we have a lot of viewType
on the adapter.
You can find the detailed solution in this article:
The main idea of this approach was to create an abstract class (AdapterDelegate
) that any adapter will implement. But before we begin the abstract class, we need to create an interface that will be used to define our model and a DiffUtilCallback
. Take a look at the snippet below:
Implement the above interface to our model
And create a DiffUtilCallback
to help us calculate the diff in our list and make updating our list more intelligent and straightforward.
Finally, we can start creating our abstract class DelegateAdapter
and then, continue to Implement abstract AdapterDelegate to our Adapter.
And our MainAdapter will look like this:
And last but not least, implemented it in our MainActivity
like this:
We don’t need to change our MainAdapter
using this approach. Which means, have to follow the O in the S.O.L.I.D principle.
Objects or entities should be open for extension but closed for modification.
When we need to add another View to the adapter, we need to add a new model and implement it with our previous interface DelegateAdapterItem
and create an adapter class that implements DelegateAdapter
.
Just like that, we can add more item views to our adapter without worrying it will look like a monster!
Another Way to Fix the Problem
As I mentioned in the previous section, Hannes Dorfmann has created a library to fix the “adapter hell” problem.
To implement it, we can choose the suitable one for our project.
Since my project has using ViewBinding. So, I just need to implement this in my build.gradle
And for using that lib, firstly, we need to create an interface for our model
then, create our model and implement it with the interface
next, create a function to handle the adapter delegate for our layout
our MainAdapter will look like this
finally, implement the adapter on our activity
This approach has the same goal as the previous approach; I like to use the AdapterDelegates library from Hannes Dorfmann because we need to implement it, and voila, all the complex works done by the library.
But if you want to understand the concept, I suggest creating your own adapter delegate as I described in the first approach.
Conclusion
The list has been an essential component in our app, as it shows a large amount of data on a page. However, maintaining a list can be a problem when having more than one view type. The adapter delegate comes to save our time solving these issues.
You can take a look at my playground project here:
References
About The Author
I am an Android Developer who loves the world of arts. I work as an Android Developer, but sometimes I do a design challenge with my friends to fill my spare time.