AndroidPub
Published in

AndroidPub

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.

Dynamic Expandable RecyclerView

TL;DR
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:

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:

  1. The constructor of the Adapter gets called which initializes our desired variables.
  2. 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.
  3. 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.
  4. 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.

Enhancements:
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-

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.

Let’s be the friends on LinkedIn, Medium, Twitter and GitHub.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rohit Surwase

Rohit Surwase

Techie by Birth. Writer by Hobby. Observer by Nature. Unpredictable by Character. Android Developer (Google Certified) by Profession.