Android Expandable RecyclerView

Sourabh Gupta
2 min readSep 2, 2018

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

The above sample has been deprecated. Please use below mentioned ExpanableLayout instead.

Original Sample Repo:

--

--