Use MergeAdapter in Android

Tram Ho

Suppose if you were given the following user interface and you were asked to design it, what would be the first thought that came to your mind?

You can come up with a variety of different ideas to implement it. But the most common idea would be to use RecyclerView with multiple view type types in a RecyclerView .

But with the solution given above, the problem that arises is that managing multiple view types is quite complicated. In the above example, we have only three types of view types. But in reality, you might end up managing a lot of view types, not just three.

So how can we design a view easily without having to manage different view types?

MergeAdapter is the solution to this problem.

In this article, we will use MergeAdapter to build the screen above.

Step 01

First, let’s add the dependency of the merge adapter (which is part of recyclerView) into the application’s build.gradle :

Step 02

For the screen above, we have three different types of View. So we need to create three different data class – User, MyDetail, Banner.

Step 03

In this section, we will create a data source for the user list to display the list. Therefore, we will create an object of DataSource and where:

Where getUser() will be used to retrieve the user list and getBanner() is used to retrieve banner images from the drawable directory.

Step 04

In this step, we will create 3 adapters for 3 types of View needed.

To display MyDetails, we will create MyDetailAdapter :

This adapter will be used to display MyUserDetails, which is basically the first part of the list. Here, R.layout.item_layout_my_detail would look like this:

Then, to display the user list, we will create another adapter ods, UsersAdapter:

This adapter will be used to display the user list, where R.layout.item_layout will look like this:

And finally, we will create an adapter to display that banner as BannerAdapter:

File R.layout.item_layout_banner :

We have now completed the design of three adapters to display three different types of views on the reycler view.

Step 05

Now, to be able to use the adapters we created in the MergeAdapter , we will first create MainActivity as the launcher activity:

File R.layout.activity_main:

Now, we need to add 4 variables in MainActivity:

we will also create another variable that will be structured according to the MyDetail data layer and contain detailed user information,

Next, we need to create a method to initialize things related to recycler view:

Here, we will initialize the adapters and transfer the necessary data for each adapter.

Note : The order that we pass into the adapters will determine the order shown on the list of corresponding views on the recycler view.

When you run the application, you will have the interface you want:

Here’s how you can design recyclerView with multiple view types sequentially using data from individual adapters.

Points to remember:

  • Suppose if we need to use one type of adapter multiple times in the recycler view, we can create multiple instance that adapter and pass it to the MergeAdapter ‘s MergeAdapter .
  • We should implement logic in each adapter separately.
  • When we update data in any adapter using notifyDataSetChanged() , the merge adapter will also call its notifyDataSetChanged() .
  • Instead of passing on adapters separately, you can pass in a list of adapters, for example:

And inside the constructor, we can pass the following:

Let’s talk more about another use case

  1. In general, we use a view holder for an adapter. But in some cases when we need to reuse the view holder for multiple adapters, we need to pass in a config in the constructor, where we must set isolateViewTypes to false .

To create a config:

and in the MergeAdapter, we pass the following:

Similarly here, we can pass a list of adapters:

  1. Suppose, we do not want to pass the adapter into the constructor but want to add it at any time into the MergeAdapter , we can do the following:

This will add the adapter to the bottom of the list. If you want to add this adapter to a specific location, do the following:

Here, we have added an adapter at position 0, which means starting the list. Now, recyclerView will look like this:

  1. Similarly, to delete an adapter:

  1. To retrieve the number of items in recyclerView from all the adapters we use:

  1. To retrieve the list of added adapters, use:

This is how you can use MergeAdapter instead of managing multiple view types in recycler view.


Share the news now

Source : Viblo