Posted on

Android RecyclerView with Data Binding Library

android recyclerview data binding

Hello there, in this post we’ll look at the Android Data Binding Library which we’re all excited about and implement a RecyclerView Adapter with it. Since there’s not so many resources and samples on the web I think that for some of you this post would be very useful and interesting. So let’s get to it.

Setting Data

I don’t want to write all the basics since they’re all easy to find in official Data Binding Guide. But first I’d like to talk about little changes in the way you do it with Data Binding Library that you need to know. There are two approaches available to set data to you views.

First approach

The first one is pretty similar to the regular, but you don’t need any Casts or findViewById. All the logic stays in Java

Where ‘name’ is your view id in XML file. You just keep a binding reference received from DataBindingUtil class as a field and you can access any of your views anywhere. But it actually does not bind anything and we want something fancy, right? That leads us to the second approach.

Second approach

Let’s clarify, to bind data to layout you need to have data that you want to display. Now what you were doing before is for each distinct value you were calling methods like setText(), setImageBitmap() etc to update views. To use Data Binding at full of its potential you need to have a model, which has lots of fields. A model like let’s say Movie, that class has 5 fields: name, posterUrl, trailerUrl, imdbRating, releaseDate. If you don’t have a model and you don’t want/need to create one for displaying data you can stick with the first approach. There’s no need for you to Bind Data, it will be the same number of lines of code, even more – extra in your layout.

Ok, but what if you already have a model and for each of the fields to display you call

what if I say that you can replace all of that with just one line of code? Like this

much better, right? And Data Binding library will take care of all the rest for you!

So let’s looks how we do it. First add a variable tag in layout XML file.

‘movie’ in here is an object name and ‘type’ is your class*

and setting it onCreate() of your Activity let’s say by

setMovie()  method passes an object reference to your layout and you’re done. Now if your movie objects are static (no changes are going to happen in fields) then that’s it, but what if you change fields of your models and want your views to display the current values? That’s when you need an Observable interface

Observable interface

By default Data Binding won’t handle field changes for you, even if you update a movie object fields, it won’t be updated in views. Your model needs to implement Observable interface, and there’s already an implementation of it – BaseObservable where you don’t need to implement anything. The only thing you need is to add notifyChange() when you change any of the fields.

Now notifyChange() actually notifies that all the fields were changed. To notify a particular field change (which is recommended) you need to have an id of that field – add @Bindable annotation to field getter and make a project, this generates an id for your field in BR (BindingRegistry)class. Then call

this is what your would look like now

Now all changes to your model will be displayed in views. But there’s one more thing: view attributes like ‘android:text’ is your substitute of Java method setText(), but I have a posterUrl in the model field and there’s no such attribute of ImageView as ‘android:imageUrl’, right? We’re going to talk about it in the next post, no worries.

I recommend using second approach very much if you have models, especially if you fetch data from server using Retrofit which forces you to use models. Then all you need in Java is to call binding.setModelName(model). Well, almost.

If you’re confused sometimes about implementing the second approach, just use the first one. It’s still much better than findViewById.

Implementing RecyclerView.Adapter

It’s actually pretty simple to do, the only things you need to change from regular approach is inflating layout

Your list_item.xml

Now if you noticed we have String.valueOf() method in here, that’s because it needs a String and will crash otherwise

and then instead of calling bunch of methods in onBindViewHolder method you set the layout variable by

Implementing RecyclerView.ViewHolder

And your Holder looks looks this

That’s it! No more code required for you to write. Great, isn’t it? Now, if you want to handle clicks — just add

You get a model object for current row by binding.getMovie() and you can make any changes or get data. For example call

and your view will be updated.

Most of all, I want to say why you should use Data Binding: it’s type-safe! Which means that you will never get a ClassCastExeption. Yes, it sounds silly, like how can you cast it to a wrong class, you obviously know what kind of class you cast it to. But when there’s an opportunity – there’s a chance, a chance of CCE, especially if this Activity is rarely used, it means your bug could not be discovered for a long time.

So that’s it, it’s about me learning the technology and showing you guys the very basics you need to know to start implementing it by yourself without the additional bunch of code. If you like to see the GitHub sample you can get it here.

Here’s some useful resources:

Data Binding Guide

Android dev summit video on Data Binding

more video


Thanks for reading and don’t forget to subscribe to my newsletter if you like and #BuildBetterApps!