android tutorial - Recyclerview with databinding in android | Developer android - android app development - android studio - android app developement
Recyclerview with databinding in android
Here is a generic ViewHolder class that you can use with any DataBinding layout. Here an instance of particular ViewDataBinding class is created using the inflated View object and DataBindingUtil utility class.
After creating this class you can use the <layout> in your layout file to enable databinding for that layout like this:
and here is your sample dataModel:
By default, Android Data Binding library generates a ViewDataBinding class based on the layout file name, converting it to Pascal case and suffixing "Binding" to it. For this example it would be MyItemBinding for the layout file my_item.xml. That Binding class would also have a setter method to set the object defined as data in the layout file(ItemModel for this example). Now that we have all the pieces we can implement our adapter like this: