Introducing MergeAdapter

Tram Ho

Introduce

MergeAdapter is a class available in recyclerview:1.2.0-alpha02 that allows you to sequentially combine multiple adapters to display in a RecyclerView . This allows you to pack adapters better than having to combine multiple data sources into one adapter (using multiple ViewHolder ), keeping them consistent and ViewHolder .

MergeAdapter allows to display the content of multiple adapters in a sequence. For example, we have 3 adapters as follows:

recyclerView will display the items of each adapter sequentially.

Using different adapters allows us to better separate the links of each consecutive part in a list . For example, if we want to display a title , we don’t need to write logic to handle hiding / displaying the title for the same item in an adapter. Instead, we can declare an adapter that contains the item displaying the title .

RecyclerView and data of the adapters

For example

We create a screen showing a basic list with Fruit , Flower and Animal objects with the following simple properties:

Corresponding to each object is an adapter. We will deploy 3 adapters as follows:

Using with MergeAdapter we get the following result:

Share the news now

Source : Viblo