DataBinding with RecyclerView

In this series of DataBinding, we have discussed the basics of this with an example.

If you do not have a basic understanding of DataBinding, then go to Introduction to DataBinding  and  Introduction to DataBinding-2 . These two should be enough for you to proceed in this discussion.

In this tutorial, we will take databinding to the next step i.e Implementing DataBindig with RecyclerView.

Okay, without wasting any time Let's get started.

Today, we are going to show a list of animals, their nature and their image in recyclerview by implementing databinding. 

As per MVVM pattern, we are going to create our model class.

Here the loadImage is our custom function to load the image into the ImageView.

Now create a layout which we will show in recycler view

Here root tag is <layout> and inside root we have created a variable of Model Class, using this variable we have added the properties to widgets.

Now create a list of animals which we will be shown inside our recycler view.


Here inside for loop, we are creating an animal object everytime and settings their respective data and finally added each animal object to Arraylist. 

Now to show this list in recyclerview we need some kind of bridge. Let's create that bridge(Adapter).


If you see onBindViewHolder () function, this binding variable is in recycler_layout.xml. So here, we are using this variable  and setting the data from list to layout.

Finally, in the MainActivity class set the call the recycler view and set the adapter to it.




That's it, We have implemented the DataBinding to RecyclerView


Comments