ConcatAdapter: Merge or combine multiple adapters into single ConcatAdapter

Ajinkya Saswade
Globant
Published in
3 min readApr 12, 2020
Photo by Max Duzij on Unsplash

When dealing with complex UI or with multiViewType scenarios in RecyclerView we used to combine many data sources into a single adapter then according to the business logic we used to distinguish them in adapter and set the UI accordingly. Many a time even for a simple header footer logic we need to merge it into single source and then write another logic for segregating it. Writing the segrate logic was the repetitive work which I guess almost all the developers must have done.

Now d.android.com has introduced the ConcatAdapter, in this we can separate the multiViewType UIs in different adapters which later can be merged sequentially into a single adapter (called ConcatAdapter). Implementing this will help in splitting the adapter logic into its own respective adapter which will make the code more reusable and less bug free.

Here is the structure of the list with multiple adapters

RecyclerView and Adapter data

ConcatAdapter is a new class available in recyclerview:1.2.0-alpha02 which enables you to sequentially combine multiple adapters to be displayed in a single RecyclerView. Implementing this is very simple. We will take an example where we will be having a list with header and footer.

If we implement the above example in the existing way then we would add the header and footer logic into a single adapter and then inside the adapter we would differentiate it with the help of getItemViewType .

The above stuff need not to be done in the new ConcatAdapter. Below is the example of it.

Header Adapter

HeaderAdapter

Header adapter contains the business logic of the header only. In the exisiting way this logic would have been in the adapter that has the logic of handling the list items. Moving ahead below is the Adapter that has the list items apart from header and footer.

List Adapter

ListAdapter

The above list adapter is very clean and contains only the logic of handling list data. Now moving ahead to footer adapter.

Footer Adapter

In this example I’m using the same adapter of header so there is no different adapter for footer. Only thing is I’m creating another instance of header adapter for footer. Now moving towards the Activity in which we will be setting the adapter to the recyclerView

Activity

Here is the demo of how it looks when compiled and executed.

Demo

Thank you, below is the code for the above example implemented with Concat Adapter.

More about ConcatAdapter

For more info about this topic you can visit the below link

I would like to thank Florina Muntenescu 🙏 for giving us an update on the ConcatAdapter topic. And I hope the above example helps you to understand ConcatAdapter. If there is anything which needs to be corrected above please let me know so that I can correct it.

Also if you find out something useful in this article, don’t forget to give a👏🏼
And if you’d like to get in touch, feel free to say hello through any of the social links below.

--

--