Get Expandable RecyclerView In A Simple Way
I’ll explain the basic working of the ViewHolder and how to use it to get the expandable item layout in a simple way.
The basic idea is to calculate the maximum number of child items present in our collection and create views for it in onCreateViewHolder() function. Later on get the actual count of child items present at the current position and handle the visibility of all extra child views if there are any, in onBindViewHolder() function.
In the world of Android Apps, we like to take care of our users. We always try to get what our users want. Designing a clutter-free App is a part of it. So we do try to show as minimum and important data as possible. Expandable layouts are one of the ways to get user’s focus on important things first while hiding less important things behind.
RecyclerView is our best friend to load a list of data efficiently. But what if we want the expandable layout functionality for each item in our list. Haha… you already know that there are many ways to achieve expandable layout functionality in RecyclerView. But the tricky part is that you will need to add either an external library or write your own code. But today I am gonna show you a simple way.
Also, check out few other articles by me:
Android Kotlin Coroutines: Basic Terminologies & Usage
The common terms used in Coroutines. Knowing these terms in advance will ease your learning process.
Best Architecture For Android : MVI + LiveData + ViewModel = ❤️
Best of MVVM and MVI architecture patterns merged into one, for a perfect architecture for any Android project.
Okay, wait… let’s first see how does our RecyclerView.Adapter work?
Whenever we call setAdapter(), the execution sequence of different methods of the Adapter is as follows:
- The constructor of the Adapter gets called which initializes our desired variables.
- onCreateViewHolder() function is called to create a new view and it’s ViewHolder and initializes some private fields to be used by RecyclerView. The important thing to be noted here is that this function is called only when we really need to create a new view.
- onBindViewHolder() function is called by RecyclerView to load the data at the specified position. This is where we will pass our data to our empty views.
- getItemCount() returns the total number of items in the collection that contains the items we want to display.
That’s it, this is how basically a RecyclerView.Adapter works inside.
Let’s get back to our example, for this example onCreateViewHolder() function is the place where all important logic is gonna happen.
Here is our DummyParentDataItem model class which includes the list of DummyChildDataItem model.
Now we are going to calculate the maximum number of child items present in the collection in onCreateViewHolder().
As we have found the max count, we will create the views for it dynamically and add it to the LinearLayout.
In this way the final view for an item is ready but wait there may be a case that each parent can have a different number of child items, so we need to handle extra child item’s visibility in onBindViewHolder().
We are done! Simple, right? Yeah, it is so simple.
For complete source code checkout this dynamic-recycler-view example.
In the given example I have not used Listeners for click events but directly the ViewHolder. And if you want to animate the visibility of the child views you can refer this animations-simplified example.
If you like the article, clap clap clap 👏👏👏 as many times as you can. It inspires me to come up with more beautiful articles.
LIKED! Share on Facebook, LinkedIn, WhatsApp.
Further Reading: Also check out a better way to get onItemClickListener & getAdapterPosition() in RecyclerView-
Android RecyclerView onItemClickListener & getAdapterPosition(): A Better Way
A lighter way to get that thing done smoothly.
LIKED SO MUCH! Medium allows up to 50 claps.
Also post a Tweet on Twitter.
About: Rohit Surwase,
Techy by Birth. Writer by Hobby. Observer by Nature. Unpredictable by Character. Android Developer (Google Certified) by Profession.