How to use Android DataBinding with RecyclerView

Robots & Pencils
RoboPress
Published in
3 min readNov 17, 2015

--

You’ve started working with the DataBinding library for Android. Everything is going well until, suddenly, you hit a wall. You need to use DataBinding with RecyclerView, but how?

Our Android team devised a solution that treats the Bindings as viewholders themselves, allowing quick changes to the Binding to be reflected easily inside your list elements.

To follow this method, first, implement an abstract class that works with the Bindings.

BindingAdapter.java:

In the onCreateViewHolder() method you can see how the >DataBindingUtil.inflate() method is called to get the binding instance, which is, in turn, used to initialize the BindingViewHolder instance. Then, in the onBindBiewHolder() method, the binding is retrieved from the view holder, and the abstractupdateBinding() method, which you will implement, will be called with that binding and the current position.

Next you will need to implement the ViewHolder to use with this adapter.

BindingViewHolder.java:

So, this should be pretty simple: the ViewHolder takes a ViewDataBinding subclass and has a constructor that takes this layout binding as a parameter. It simply calls the superclass with the layoutBinding.getRoot() to set up the view relation, then keeps a reference to the binding.

This class also implements a public getBinding() method, this method is called by the BindingAdapter inside its onBindViewHolder() method implementation as discussed above.

How would we go about using these two classes in code to populate a RecyclerView with data?

Sample implementation:

As usual, you start by setting the layout manager that the RecyclerView will use. Then you create an anonymous class instance of BindingAdapter. Here you use the DataBinding class as the generic type, and pass the layout resource id as the constructor paramater. You’ll need to implement the updateBinding() abstract method.

Inside updateBinding() you get a reference to the row’s binding, and the position. So, just look up the actual object in your data model — here a simple list of OfferViewModel called mOfferList is being used — and update all of your binding properties.

We are using Retrolambda to support lambda notation inside the setHandler call here, which is a quick and easy way to set up an onClick handler inside the layouts that performs the action that you desire.

For reference, below are the actual layout XML files being used.

layout_offer.xml:

layout_offer_call_to_action.xml:

Of note in these implementations is the data binding library handles nested included layouts, and passing bindings from one to the next using the bind:* notation. Otherwise the bindings here are very simple, setting text or defining handlers.

We hope this helps when you’re using DataBinding in your next Android app and come across the need to also handle RecyclerView adapters.

--

--

A digital innovation firm. We help our clients use mobile, web & frontier technologies to transform their businesses and create what’s next.