Expandable RecyclerView
Custom RecyclerViewAdapters for expanding and collapsing groups with support for multiple view types.
Download
ExpandableRecyclerView:
ExpandableCheckRecyclerView:
Usage
Let's say you are a rock star :guitar: and you want to build an app to show a list of your favorite Genre
s with a list of their top Artist
s.
First, define your custom ExpandableGroup
class:
Next up, let's create the ChildViewHolder
and GroupViewHolder
. These are both wrappers around regular ol' RecyclerView.ViewHolder
s so implement any view inflation and binding methods you may need.
Now we are ready for the juicy part - let's make our ExpandableRecyclerViewAdapter
By including your GroupViewHolder
and ChildViewHolder
in the definition of the class, you'll see that the onCreateGroupViewHolder
and onCreateChildViewHolder
methods return the correct type :thumbsup:
Lastly let's you'll need either an Activity
or Fragment
to host your adapter. Once you've got that up and running, all that's left if to instantiate your fancy new GenreAdapter
with a List<Genre>
Saving And Restoring Expand / Collapse State
If you want to save the expand and collapse state of your adapter, you have to explicitly call through to the adapters onSaveInstanceState()
and onRestoreInstanceState()
in the calling Activity
Adding Custom Expand / Collapse Animations
If you want to add a custom Drawable
that animates based on a groups state, override the expand()
and collapse()
methods in your GroupViewHolder
:
Listening to Expand/Collapse events
If you want register an ExpandCollapseListener
outside of the adapter, you can simply call setOnGroupExpandCollapseListener
on the ExpandableRecyclerViewAdapter
Multiple Child and Group Types
The MultiTypeExpandableRecyclerViewAdapter
allows subclasses to implement multiple different view types for both children and groups.
Continuing with our genre example, let's say you wanted to display regular artists differently from your favorite artists. Let's start by making a new FavoriteArtistViewHolder
Just like the regular ArtistViewHolder
, FavoriteArtistViewHolder
must extends ChildViewHolder
.
Next up, let's create a subclass of MultiTypeExpandableRecyclerViewAdapter
called MultiTypeGenreAdapter
and let's add two static int
s representing our two artist view types:
Notice we started used values > 2. That's because ExpandableListPosition.CHILD
and ExpandableListPositon.GROUP
are 1
and 2
respectively so they are already taken.
Since we only want a single view type for groups, we only need to override getChildViewType()
. As getGroupViewType()
will default to ExpandableListPosition.GROUP
.
Since we provided custom view types for our children, we must also override isChild()
And now, just like in any other RecyclerView.Adapter
in our onCreateChildViewHolder
and our onBindChildViewHolder
we can use the provided parameters to switch on the different view tyeps:
Expandable Check RecyclerView
An extension of expandablerecyclerview
for checking single or multiple children within a group
The setup for the single and multi check versions is very similar to the expandablerecyclerview
we walked through above. Here are a few of the notable differences...
CheckedExpandableGroup
Instead of ExpandableGroup
you must use CheckedExpandableGroup
. CheckedExpandableGroup
is a subclass of ExpandableGroup
that uses a SparseBooleanArray
to hold onto which of it's children are checked.
The expandablecheckrecyclerview
library comes with two default implementations - SingleCheckExpandableGroup
and MultiCheckExpandableGroup
.
Clearing Choices
The CheckableChildRecyclerViewAdapter
has a clearChoices()
which un checks any currently checked children.
CheckableChildViewHolder
The CheckableChildViewHolder
is a subclass of ChildViewHolder
that has a Checkable
widget. The Checkable
interface is initially not set, so in order to see your children view states update, you must set a View
that implements Checkable
in your view holder.
Listening to Child Click Events
There is a custom callback for click events on children of a CheckedExpandableGroup
which returns you the View
of the row item that was clicked, the current checked state of the child, the containing CheckedExpandableGroup
group and the index of the child that was clicked.
Sample App
To see the complete code for all the above examples along with unit tests for the adapters check out the sample
app. The app has the following packages:
expand
An example of basic ExpandableRecyclerViewAdapter

multicheck
An example of a CheckableChildRecyclerViewAdapter
using MultiCheckExpandableGroup

single check
An example of a CheckableChildRecyclerViewAdapter
using SingleCheckExpandableGroup

multi type
An example of a MultiTypeExpandableRecyclerViewAdapter
using two different child view holders
