Single Adapter for any RecyclerView

Sanoop Sashindran
FinBox
Published in
3 min readMar 12, 2019
This image has nothing to do with the post. It’s just that this pie looked good !!

First of all any android app without RecyclerView is not an android app. I’m sorry to say this.

Since that is out of the way now we can concentrate on what the title is trying to say.

As android developers, we usually have to deal with a list of data that will show different contents and/or different contents that can have different actions. So for each list, we usually have a different adapter that does the same thing like every other adapter i.e. show the item and perform actions on the item.

So in one Activity if you have to show 3 different lists. That means you will have to create 3 unique adapter that caters to these lists. What if you could handle any list with just one adapter. That thought sounds cool and made me write this article. I hope you find it useful, any feedback would be a great help.

Okay, let’s dive right in!!

Before we go ahead you will need to know about DataBinding to implement this. Just the basics will be enough to understand this article.

Let’s note down what we need to create a generic adapter

  1. Row layout
  2. List of items
  3. Event handler (To handle click events from the list)

Keeping this in mind lets me show you how the generic adapter will look like.

If we look at the code it looks like just any other RecyclerView Adapter. The only difference is in the layoutId and items. These are the inputs that we need, to inflate any row item.

onCustomClickItemListner is our event handler that will propagate any click events on the list.

We take a generic list i.e. List<*> as our items array. In Databinding we consider a Row layout to be mapped to a model. So any update in the model should change the row attribute by the awesome two way binding feature. So this is how a sample row layout will look like.

This is a simple row with 2 TextView next to each other. As you can see in the <data> tag we have set 3 variables.

The first variable is the Model that this row represents. This model object is used to set the text in the TextView.

The second variable is the ViewHolder that is needed to handle all the events that you have in your row layouts. If I want to handle row item click event I can add this line in the parent layout

android:onClick=”@{(v) -> handler.onCustomClick(v, position)}”

onCustomClick is the function inside the BaseViewHolder that will send the event to the parent activity.

Third Variable is the position of the list item that is used to let the parent know which item was clicked.

That’s all you need to set your generic Adapter. This is how you will use it in your activity

In your onCreate or where you define your view, you can just add the variables that are required by the adapter i.e. Layout ID and Product List

Handle Events sent from adapter

With these changes, you can have just one adapter to handle any type of list UI.

Hope you found this article useful. You can take it to the next level by creating a BindingAdapter that will take the layout and the product list from the XML itself. Which will reduce more code from your activity.

Thanks for reading. :)

--

--