DEV Community

Cover image for Using RecyclerView with ViewBinding in Android via Kotlin
Aayush Gupta
Aayush Gupta

Posted on • Updated on

Using RecyclerView with ViewBinding in Android via Kotlin

RecyclerView is one of the most used libraries in Android. In this article, I will show you how you can use it in conjunction with ViewBinding to make the most of it.

Dependencies

Here are the required dependencies:

  • ViewBinding enabled in build.gradle file of the module,
  • RecyclerView widget added to your host Fragment/Activity, and
  • A XML layout that will be used by RecyclerView's Adapter class to inflate the host Fragment/Activity.

In case you are unfamiliar with the topic of ViewBinding, you can check out my article on it.

Implementation

We will create a class for our Adapter and extend it to RecyclerView.Adapter class while instantiating it.

This requires us to create a custom ViewHolder class that extends to RecyclerView.ViewHolder and will take our layout's binding as an argument used to get the root of the layout file while at it.

class RVExample(val list: ArrayList<Example>) : RecyclerView.Adapter<RVExample.ViewHolder>() {

    inner class ViewHolder(val binding: RVExampleBinding) : RecyclerView.ViewHolder(binding.root)
}
Enter fullscreen mode Exit fullscreen mode

In the above example code, I am accepting a list as the class constructor. This list is supposed to have multiple elements of type Example which will have text as a key which we will use later.

Notice how the inner class's constructor is of type RVExampleBinding. You need to make sure that you use the layout resource you created earlier to be consumed by the Adapter class.

Now, we need to override the class functions in order to complete the Adapter setup.

The first method we will override is onCreateViewHolder. This function will return an instance of the ViewHolder class we created above. As the class constructor requires us to pass the required Binding, we will use pass the same while inflating the layout.

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    return ViewHolder(
        RvExampleBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )
}
Enter fullscreen mode Exit fullscreen mode

The second method we will override is getItemCount. This function will return the size of the list we accepted as an argument in the Adapter class.

override fun getItemCount(): Int {
    return list.size
}
Enter fullscreen mode Exit fullscreen mode

The third and the last method we will override is onBindViewHolder. In this method, we will use the holder variable which we accepted as an argument to access the GUI components using binding. position argument will help us to access the element at the specific position in the list.

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.binding.tvExample.text = list[position].text
}
Enter fullscreen mode Exit fullscreen mode

Here is how my Adapter class RVExample.kt looks like:

package dev.theimpulson.rvexample

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import dev.theimpulson.rvexample.databinding.RvExampleBinding
import dev.theimpulson.rvexample.Example

class RVExample(val list: ArrayList<Example>) : RecyclerView.Adapter<RVExample.ViewHolder>() {

    inner class ViewHolder(val binding: RvExampleBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            RvExampleBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

   override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.binding.tvExample.text = list[position].text
    }

    override fun getItemCount(): Int {
        return list.size
    }
}
Enter fullscreen mode Exit fullscreen mode

and that's it. Now you can use RecyclerView with ViewBinding and take the benefit of null and type safety offered by ViewBinding.

Top comments (1)

Collapse
 
maanuanubhav999 profile image
anubhav_sharma

Thanks it helped a lot.