8

Abstract RecyclerView Adapter to Eliminate Some Boiler-plate Code

 3 years ago
source link: https://www.simplifiedcoding.net/abstract-recyclerview-adapter/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Abstract RecyclerView Adapter to Eliminate Some Boiler-plate Code

Hi everyone, in this post we will learn about making an Abstract RecyclerView Adapter. Abstraction is an object oriented programming concept, and we all learn it in our books. But the irony is most of us don’t use the power of it.  Even I didn’t pay much attention to these OOP design concepts in my previous tutorials. But now I think that the practical use of these concepts should be taught to everyone.

We all make RecyclerView for displaying a list in our application. And you already know that for making a RecyclerView we create a RecyclerViewAdapter. For each RecyclerView we create Adapter and the adapter contains same code again and again, but with the help of abstraction we can eliminate a lot of code to make our adapter concise.

In this post I will be using one of my previous tutorials, you can see the video below.

And a request to you all, if you like my content, please subscribe to my YouTube channel. 

Building an Abstract RecyclerView Adapter

So we will build an abstract RecyclerView adapter and after building it, we will be using the same adapter to the other RecyclerView adapters, whenever required. So what we are making here will be used as a Base class for all our RecyclerViewAdapters.

In the existing project that you can see in the above video, we will create a class named BaseRecyclerViewAdapter.

abstract class BaseRecyclerViewAdapter<T : Any, VB : ViewDataBinding>
    : RecyclerView.Adapter<BaseRecyclerViewAdapter.Companion.BaseViewHolder<VB>>() {
    var items = mutableListOf<T>()
    fun addItems(items: List<T>) {
        this.items = items as MutableList<T>
        notifyDataSetChanged()
    var listener: ((view: View, item: T, position: Int) -> Unit)? = null
    abstract fun getLayout(): Int
    override fun getItemCount() = items.size
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = BaseViewHolder<VB>(
        DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            getLayout(),
            parent,
            false
    companion object {
        class BaseViewHolder<VB : ViewDataBinding>(val binding: VB) :
            RecyclerView.ViewHolder(binding.root)
  • The common operations, that we perform in all our RecyclerViewAdapter, we will put inside this class. And then instead of using the RecyclerView.Adapter we will use this class in our RecyclerViewAdapter classes.
  • You can see below; an example of using this class as an RecyclerViewAdapter class.
class ProductsAdapter : BaseRecyclerViewAdapter<Product, ProductLayoutBinding>() {
    override fun getLayout() = R.layout.product_layout
    override fun onBindViewHolder(
        holder: Companion.BaseViewHolder<ProductLayoutBinding>,
        position: Int
        holder.binding.product = items[position]
        holder.binding.root.setOnClickListener {
            listener?.invoke(it, items[position], position)
  • Now if you compare this adapter to your previous adapter, you can see how concise our class is.

Abstract RecyclerView Adapter – Video Explanation

You can check a complete step by step video explanation of the concept from below given video.

Abstract RecyclerView Adapter – Source Code

For more clarity you can get a working project code from the link given below.

Source Code Download
git hub repository
Get the Source Code

If you liked this post, then please share it with your friends. Thank You

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Checkout these tutorials as well:

  • Android Jetpack Paging 3.0 Tutorial - Paged RecyclerView using Retrofit
  • QR Code Reader Android Kotlin Tutorial using ML Kit

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK