RecyclerView With Android DataBinding

In I/O 2015 Google announced a data binding library for Android. With Data Binding, you create an ongoing link between an element in the user interface and a value. Data Binding is the process that establishes a connection between the application UI and business logic.

In this post we will learn how to integrate recyclerview with android Data Binding.

Step 1 : Enable dataBinding in your module level gradle, here is how your build.gradle should look like.

Step 2 : Create POJO/Model class called User with 3 parameters name, profile_image & age.

Step 3 : Create a layout for Recycler list items

Here user will be model class reference variable, you can have any number of variables as you need. <layout> will contain your model class information using which you will notify your controls about value they are going to show.

Step 4 : Create an adapter for Recyclerview called UserAdapter

Here
binding = DataBindingUtil.bind(itemView); will create binding for your recycler_item layout and return view.
binding.setUser(user); will set User data to recycler items.

Step 5 : Write xml code for your activity/fragment layout

Step 6 : Write your code in activity/fragment to attach adapter to Recyclerview.

Output :

RecyclerView Android DataBinding

Advance DataBinding in Android
Being More Productive With Android Studio – Part 1

  • Michel Marin

    private RecyclerItemBinding binding; does not compile. I get a “cannot resolve symbol”. And it is unknown on the Android Documentation. How do I make it compile? Which import shoule I use? Thank you.

    • Ravi

      RecyclerItemBinding is auto generated class. Make sure you have enabled DataBinding in build.gradle file.

      • Michel Marin

        Ravi, yes I do. My project is already retrieving data from Sqllite and binding it to the main_activity. It is even unknown to the Android documentation.

        • Ravi

          your class name will be related to xml name, so make sure it is correct, in above example xml name is recycler_item so generated classname is RecycerItemBinding.

  • Michel Marin

    Oh, That trick! Thanks for reminding me! It’w working so far. Thanks for the reply. I’ll let you know if I have other questions and when it will be finally working in my app.

  • Michel Marin

    Ravi, I got everything to compile except for one statement. “binding.recycler.setAdapter(adapter);” does not compile. binding does not have the recycler RecyclerView. I have 2 xml files. sac_rmv.xml with the and a sac_rmv_activity.xml with the <android.support.v7.widget.RecyclerView android:id="@+id/recycler"…. What could prevent the "recycler" from being geneated? Thanks again.

  • Michel Marin

    Ravi, I created a new project to retrace my steps and the problem is when I rename my POJO. I can fix pretty much all of the invalidated statements caused by the rename but not that binding.recycler.xxx. I havent find the magic recipe yet to rename a POJO used for databinding. I’ll post it once I find it!

    • Ravi

      binding.recycler has nothing to do with POJO, binding is just a variable of activity/fragment binding. Here it is object of ActivityMainBinding. Make sure you have “RecyclerView” having id “recycler” in your activity/fragment xml.

  • Michel Marin

    Ravi, thanks for the quick reply. If I only Refactor, Rename the POJO, yes all is fine. But if I rename the “name=xxx” in the tag for consistency then yes, binding.recycler.xxx gets affected. Try it!