Android Data Binding + ListAdapter

Tram Ho

Data Binding is a powerful library that I have spent most of my time as an Android dev (most likely because it was a bit conflicting). I read a lot about them, and by chance I met George Mount; s blogposts. That article really attracted me one thing about Data Binding on RecyclerView.

That was when I realized how great Data Binding was, and I started to become a ListAdapter user. I think things could be better if I combined them. This will be a summary of how we can make the most of Data Binding and ListAdapter to reduce by boilerplate and only make our code simpler and more beautiful.

Let’s start

… but first, just in case you don’t know what a ListAdapter is:

The RecyclerView.Adapter base class to present List data in a RecyclerView includes calculating the difference between Lists in a background thread. It is a base adapter that updates itself (with free animations!) When the list changes. Calling notifyDataSetChange and its friends is now a thing of the past – all you need to do now is call submitList () and pass the updated list. If this is new to you, it might be a good idea to take your time and learn more about it – the document is good and may actually be the only source you’ll ever need.

Now let’s get back to the important stuff

The main idea of ​​blogpost, which I mentioned above is how we can offer a generic ViewHolder that we can reuse at any Adapter in our application. I assume that ViewHolder will only need a single data object to bind (that’s definitely a goog practice). This object is the parameter of the bind () method, and all we do with it is set it as a variable with a ViewDataBinding we get as a parameter of the constructor. Through a clever naming convention, we have to be able to make this ViewHolder work wherever it wants.

Yes, this small class has the potential to see most of the VirewHolder you’ve written so far. The original code served our purpose, so we won’t change anything

Next, we can create a base Adapter that will have most of the templates we will need for any Adapter we will write. Our DataBindingAdapter will be a bit different from here because it will extend ListAdapter:

It’s a super simple class – even simpler than originally, thanks to ListAdapter. There is one important thing that we do there: the layout id is used by the Adapter to inflate the layout as the viewType, so any child Adapter will need to override getItemViewType () to show that the item should inflated. This assumption is another good practice, even reinforced in the document

Now let’s say we want to show a list of books. With the 2 classes we just wrote, let’s see what BooksAdapter will look like

Thanks to our base class, the only thing we will need to do is care about when to create an adapter and write the DiffUtil.ItemCallback required by ListAdapter and specify the layout id we want to inflate via theItemViewType. Of course we also need to pay special attention to our layout file:

The important thing here is that the variable name must match the name we use in DatabindingViewHolder- in this item is item. The rest is simple and is the basic code of Data Binding

What about the Adapter with ViewHolder is different

Great, I’m glad you asked. Usually, the only thing we have to change is the way we implement getItemViewType in the Adapter, but normally DiffUtils.ItemCallback also has to change with

Let’s say our list book has 2 sections, which will naturally be represented by some completely different layouts. We are sure that our Book model and our new Section share the interface, let’s say Listable, just because we can have them in the same list.

The BookAdapter will look like this

And nothing special when you use this adapter in an Activity as an example

What I like most is that everything is set up, creating another Adapter is as easy as writing how to distinguish items from a list (so ListAdapter can do its job) and the layout we want to inflate. . That’s boilerplate proof and it feels like we’re just writing really important code.


Through this article we have built together a very easy-to-read and concise adapter for RecyclerView. Thank you for watching.


The article has references from the source:

Share the news now

Source : Viblo