How to bind a list of items to a RecyclerView with Android Data Binding
When using MVVM architecture Android Data Binding is the best way to propagate changes to the UI. If you are completely new to Data Binding I suggest you catch up on the basics before reading this article.
Data Binding Library | Android Developers
The expression language allows you to write expressions that connect variables to the views in the layout. The Data…
There are plenty of data types that we can bind to the properties of different widgets, however when it comes to binding a list of items to a RecyclerView things are not so straightforward. You can’t just do something like this:
There is no such XML property, that binds data to a RecyclerView. We need an Adapter, what will get the list of items from a data source. In this article I show you a simple method to bind data to a RecyclerView in XML.
Let’s see the following scenario
We have a list of data coming from a backend service, that is constantly changing. Our ViewModel class requests the data and gets the updates. When we get the update, we need to update the UI with the new data. Here is the implementation of our ViewModel class.
We are using BaseObservable from the Data Binding library just for the sake of simplicity, but the same thing can be implemented with the new
ViewModel class from the Architecture Components. Also for the same reason we are not using a real backend service, we just generate a list of random numbers every second.
Our Activity also looks pretty simple:
Now we only need to update the data in the Adapter, when it changes. How can we do that? We have multiple options:
- We can pass the Adapter to the ViewModel. I don’t really like this approach, because a ViewModel class should know nothing about the Adapter.
- We can add an
viewModelinside the Activity. This is acceptable, but adds some boilerplate code to our Activity.
- We can write a custom Binding Adapter and use Data Binding.
Writing a custom Binding Adapter
With a custom Binding Adapter we can specify our own binding logic for an XML property. We will write a custom binding logic, that gets the adapter from the RecyclerView and sets the data for it.
First we need to add a
setData method to our adapter.
Now we can write our BindingAdapter.
As you can see this is really simple, but has a slight problem. This method is not too generic, and if we will have other adapters in our app, we will have to add many if-else branches.
Let’s fix this by creating a generic interface, what all of our adapters will implement, that need to use Data Binding.
You might wonder why aren’t we using a List here with a type parameter. It can work too, but this gives us more flexibility. Sometimes we need another data structure to populate a RecyclerView.
And finally we change the implementation of the BindingAdapter.
After adding these changes to our codebase we can easily bind any data to a RecyclerView using the
data XML property from the
app namespace and keep our ViewModel and Activity/Fragment classes much more cleaner.
This is how our
UserAdapter looks like after the changes.
As you can see binding data to a RecyclerView is quite simple to achieve, and it can reduce some boilerplate code from your Activity/Fragment classes, and keep your ViewModel classes clean, and if you are already using Data Binding for other widgets on the screen it is good to remain consistent.
You can find the whole example project in this Github repository:
GitHub is where people build software. More than 28 million people use GitHub to discover, fork, and contribute to over…
Thanks for reading my article.
If you liked it share it with fellow Android developers or give it a clap or two.
If you have any questions or suggestions leave a comment below.