Refactoring RecyclerView adapter to data binding

Jarosław Michalik

Jarosław Michalik

Developer, Speaker, GDG Kraków member, Organizer at SFI.org.pl, https://twitter.com/rozkminia, https://github.com/rozkminiacz

Jarosław Michalik

Jarosław Michalik

Developer, Speaker, GDG Kraków member, Organizer at SFI.org.pl, https://twitter.com/rozkminia, https://github.com/rozkminiacz

May 15, 2018 4 min read
Refactoring RecyclerView adapter to data binding
Photo by Pathum Danthanarayana on Unsplash

How many times setText() is invoked in your onBindViewHolder() method? For complex recycler items it can be really annoying. You have several lines to set texts, images etc. Data Binding allows us to shorten this stuff.

Some time ago, when Kotlin on Android was early beta, we were maintaining huge, multi-module app. Writing findViewById time after time was really annoying. While working on project we were using ButterKnife, however it couldn’t be used in non-app modules then. We tried to find proper solution — we tried several libraries, even Android Annotations framework. However none of them couldn’t meet our expectations. We were very close to start developing our own framework, which fulfill all developer needs and dreams.

Suddenly — we discovered data binding library, supported by Google:

At the first sight I wasn’t sure about using that library, and I was trying to convince team members to develop our own framework. Now, after several projects developed with data binding I am evangelising colleagues to use it instead of Kotlin extensions.

I will try to show you the process of refactoring simple recyclerview adapter.

Gradle setup

First of all, we need to setup our build.gradle.

I’ve added data binding block to Android configuration in module-level build.gradle. To use data binding with Kotlin, you may also need to define sourceSets and tell kapt (Kotlin Annotation Processor) to correct errors.

Binding layouts

Consider a simple app which displays some items in RecyclerView.

As you see, we have some boilerplate code. Especially in binding data item to itemView in ViewHolder. With data binding we will try to simplify that case.

To use data binding generated classes all we need to do is put all the view code in <layout> tags:

Sign up for Untitled Kingdom newsletter

Fresh startup & app development infobites delivered to your inbox.

➡️Check our Privacy Policy⬅️

Kotlin extensions provides us views as Kotlin variables in itemView object. However — it looks ugly. We should follow the Kotlin language guidelines and use camelcase. Data binding generated us properties in a cleaner way:

That was important when we were maintaining app in Java — amount of view boilerplate was incredible.

The data part of data binding

The most powerful concept of data binding is the 'data' part. Actually we can bind some data class to xml layout and bind item properties to fields directly.

First, let’s define our view item model:

It should be in direct relation to what we see on screen — if subtitle consists of type of promotion, and discount value — we should concatenate strings while creating ItemViewModel instance.

Sign up for Untitled Kingdom newsletter

Fresh startup & app development infobites delivered to your inbox.

➡️Check our Privacy Policy⬅️

To use data part of data binding in xml, add <data> tag in your <layout>:

Now we can refactor our adapter:

Simple logic inside layout

Field binding is not the only thing data binding has to offer. We are able to create simple logic in xml. For instance — in our adapter we still toggle visibility of special label — we can extract it to layout:

And our adapter in the will look like this:


Summary

Data binding may be a very useful tool for us, Android developers. It helps reduce boilerplate code, keep our ViewHolders thin, and of course — bind data. No more writing findViewById.

If you decide to try data binding in your project, I’ll keep my fingers crossed and wait for your feedback/updates. Happy coding!

Sign up for Untitled Kingdom newsletter

Fresh startup & app development infobites delivered to your inbox.

➡️Check our Privacy Policy⬅️

Read more from Untitled Kingdom:

0
0
Also tagged Digital Health

How much does it cost to develop a digital health app and why?

Klaudia Raczek

Klaudia Raczek

Marketer interested in startups. Building software for web & mobile apps for digital health, FemTech, IoT and digital transformation @ untitledkingdom.com

Klaudia Raczek

Klaudia Raczek

Marketer interested in startups. Building software for web & mobile apps for digital health, FemTech, IoT and digital transformation @ untitledkingdom.com

Feb 21, 2019 6 min read
Also tagged Business

What’s my purpose of running a business?

Also tagged Work Culture

Software company vs. digital product studio. What’s the difference? What questions should you ask?

Klaudia Raczek

Klaudia Raczek

Marketer interested in startups. Building software for web & mobile apps for digital health, FemTech, IoT and digital transformation @ untitledkingdom.com

Klaudia Raczek

Klaudia Raczek

Marketer interested in startups. Building software for web & mobile apps for digital health, FemTech, IoT and digital transformation @ untitledkingdom.com

Jan 07, 2019 9 min read