RecyclerAdapter with Multiple Type Rows (with DataBinding)

Murat YÜKSEKTEPE
3 min readJun 26, 2023

Hello,

In this article, I’ll try to show an answer to “How we can create multiple type RecyclerView in Android?” All our codes will be written with Kotlin for this tutorial and we’ll use DataBinding for two-way communication between layouts and logic.

We’ll create our base adapter classes and models so you can use these project wherever you want.

Before all, we must with done 2 things. Enabling dataBinding and adding the ‘kotlin-kapt’ plugin id in our app-level build.gradle file.

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
....
id "kotlin-kapt" <=====================
}

android {
....
buildFeatures {
dataBinding true <=====================
}
}

Let’s start step by step;

1 — Create a package named “adapter
2 — Create a package named “model” under the “adapter”
3 — (under ”model” package) We need to create base data model for extending other data models from this. This is an interface with the empty body;

4 — Now we have to create an abstract class for extending other “row”s which we’ll create later for each different rows. This class has 3 abstract values.

layout” for the path of layout design file (xml)

dataClass” for the class name of data model.

data” for the data that our row needs.

Our project is getting similar to this view below.

Now, go to your feature package and create “adapter”, “rows”, “data” and “row” packages like this;

5 — Create your own data models into rows -> data package for the item that you want to list in RecyclerView.

For example “TitleDataModel” and “EntryDataModel”, have to extend from “BaseDataModel

Now we can create and edit our layout files.

6 — activity_main.xml

As you know, we need to move the main layout item into <layout></layout> tag for enabled DataBinding.

Important!!

For our row’s layout, we have to add a <variable> tag that has a related data model as “type”, and the “name” value must be “data”

title_row.xml

entry_row.xml

Don’t forget!
Change the package name for the data model.

Don’t miss it!
Items get the data from “@{data.VARIABLE_NAME}”

7 — Now, we are able to create data classes for each row that are based “BaseRowModel”

Don’t forget!
layout” and “dataClass” values will override in the body, “data” value will override in the constructor.

8— Create “MultipleTypeAdapterViewHolder” class that we’ll use in our adapter as ViewHolder after.

We’ll pass the data (row.data) which was set for our row to the layout via DataBinding.

Don’t forget!
To use BR, you must add ‘kotlin-kapt’ plugin id in your build.gradle (app)

9 — Create “MultipleTypeAdapterDiffUtil” class that we’ll use in our adapter as DiffUtil.

10 — Create “MultipleTypeAdapter” class as a base adapter that is based ListAdapter.

11 — Finally, we can create an adapter class for our feature that is based “MultipleTypeAdapter

12 — In our Activity (or Fragment) we can create a list as List<BaseRowModel> and send it to our feature adapter.

13 — That is all! Hope this article is helpful for you.

Have a nice coding :)

--

--