Abstract RecyclerView Adapter to Eliminate Some Boiler-plate Code
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.
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.
- 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.
- 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
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.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK