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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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.
If you liked this post, then please share it with your friends. Thank You