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.
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.
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:
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.
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:
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!