With Google IO 2015 came with a great deal of delights for developers, one of them is the Data Binding library. This library allows us to eliminate a ton of boilerplate code such as findByViewId() calls, adding references to views (inside activities/fragments), setting listeners, etc. This support library allows us to declare custom attributes on the fly (and associate it with static methods), instead of declaring it in attrs.xml. It also supports ternary and null coalescing operators, call object methods and integration with collections framework.

In this article we will look into how to use Data Binding with RecyclerView. So lets go ahead and see how it all works.

Initialization

Make sure you are using Android Studio 1.3. Add following dependencies to your Application’s build.gradle.

dependencies {

classpath "com.android.tools.build:gradle:1.3.0-beta3"

classpath "com.android.databinding:dataBinder:1.0-rc0"

}

}

Add databinding plugin after android plugin, and make sure jcenter is in the repositories list of your sub projects.

apply plugin: 'com.android.application' apply plugin: 'com.android.databinding'



Implement RecyclerView using Data Binding API