2nd Jul 2021

Simplifying RecyclerViews with binding adapter & data binding

Mobile App

Written By, Mahek Thaker


Have you ever thought about getting rid of recyclerView adapters & managing them? Especially in the case of nested RecyclerViews.

Problems with RecyclerView’s Adapters

  • Things can get messy when we have many views to maintain where everything just gets one big pile of code which results in no code readability at all and the situation gets more tricky with updates.
  • But that is just tip of the iceberg because when you have n number of nested RecyclerViews & Adapters to maintain that can result in just big mess
  • when you have to make some updates, you would be afraid to touch the code because what you see in there is a pile of code.

Data binding & Binding Adapter joins the rescue party.

If you are new to data binding please refer to this documentation

  • How about one Adapter with all your RecyclerView needs which uses a binding adapter & data binding which sets up everything for you, increases your productivity and doesn’t create a mess.
  • This can be very helpful when working in teams so everyone can understand, change & update everything without fear of breaking some piece of code.

Code Time

Here as we can see I am taking a number of parameters which are needed to set up everything for the adapter. Let’s understand each of them.

  1. Layout Id : represents your recyclerView’s row file.
  2. Mutable List : represents your data to be added into adapter
  3. Br: represents your model class from the list
  4. Click listener : reference to clicklistener which will give you click callbacks in your activity/fragment.
  5. mapOfBr<Int,Any> : this map will have a list of br variables which will be initialized in your row file from the adapter.

In the onCreateViewHolder i am initializing binding from your layout file.

In the onBindViewHolder

  1. Your model is initialized into your layout’s model variable
  2. Map of brs is then initialized
  3. Then your clicklistener is initialized to send out all clickevents & as the reference of itemclick listener is passed on to nested RecyclerViews you will be able to get all call backs in your activity/fragment.

getItemCount() will set the count from your list.

In viewHolder class I am passing binding which is initially initialized in onCreatViewHolder

Understanding the code of the binding adapter.

Binding adapter needs three parameters but our GlobalAdapter needs four.

  1. Layout Id
  2. List
  3. Click listener
  4. For the mapOfBr I am initializing it manually from the code, initially you will pass your itemClickListener reference to your layout file from activity/fragment & incase if you have nested RecyclerViews i will set it into br.itemclick to pass on the initial reference into row file of your nestedRecyclerView.

So you will be able to get all callbacks of clicklisteners into one listener itself.

Let’s understand how this RvClickListener interface works.

  1. You implement this interface in your activity/fragment
  2. You then initialized your itemclicklistener variable into your activity/fragment
  3. You pass your itemclicklistener to bindingAdapter
  4. BindingAdapter passes it to adapter
  5. Adapter initializes it to binding file
  6. You get all your callbacks in your implemented method

The Big Picture

How everything works as a whole?

Model class

Here we have three classes News, NewsCategory & Articles classes

News & NewsCategory contains a list which is displayed in recyclerView.


Here View.OnClickListener, RvClickListener & model which is then initialized with variables in your row/item layout file.

Here you’ll get a cleaner way to handle clicks because each view will be different & each view will have different model classes based on the layout so you’ll be able to handle clicks easily.

Note: Updating data also needs data to be updated from your model too so if your data source is api or database you need to call it again when you delete or update the data, otherwise data still remains in your model & thus even after deleting it from adapter it will be added again by binding adapter.


Here in the layout file we have 3 variables as we had in MainActivity. As we pass this data in our binding adapter with the layout file it will then initialize GlobarAdapter.

Now let’s see how the nested RecyclerView is going to be initialized.


Here we have the same three variables which we had before, if we recall the code from GlobalAdapter’s onCreateViewHolder, we were initializing the same three variables from there according to positions thus we’ll have our variables initialized here.

Now we can further initialize another recyclerView in this row file the same way we did for the first one.

Now in the nested row file I am simply displaying the data.

You can have multiple recyclerViews here and there & it totally considers

each of them independently so you don’t really have something like a parent child concept applied here.

Here is the final output


All the source code is available at this Github repo, pull requests are always welcomed.


This approach can indeed help many android developers out there & sometimes things can get tricky in the Android world, so things might come handy.

Thanks for your time, see you around folks.

Written By,

Android Developer at Yudiz Solutions Pvt. Ltd