Android Expandable RecyclerView

Sourabh Gupta
Sep 2, 2018 · 2 min read

RecyclerView is the most used component in Android for making lists. But many of the times, we as developers find ourselves having to implement a list that actually expands smoothly. Since Android doesn’t provide any such component for smooth expand/collapse feature in list and many libraries out there makes the ViewHolder non-recyclable to achieve this functionality. I have created a sample project to demonstrate how we can achieve it without writing a lot of code and without making the ViewHolder useless. So, here is how it works.

First of all, Create a Custom View that knows how to animate to expand and collapsed state. In this case, I am making a ExpandableLinearLayout.

This is the code that animates the view smoothly.

Second, Create a modal that keeps track of the state of your views in RecyclerView.

public class ExpandModel {
private boolean expanded;

public void setExpanded(boolean expanded) {
this.expanded = expanded;
}

public boolean isExpanded() {
return expanded;
}
}

Finally, add this code to your Adapter and ViewHolder to make the animation work.

Now plug the Adapter into Recyclerview.

And That’s it. Now you have completely working Expandable Recycler View.

Fork me on GitHub

Check out the complete source code in the following link:

Sourabh Gupta

Written by

Android Developer at Totality Corp.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade