Android App Development

Expandable RecyclerView in Android

Expandable RecyclerView

Expandable RecyclerView is one of the most important feature in Android which can be easily created for our application using Big Nerd Ranch’s Expandable RecyclerView Library

It contains two views one is parent view and other is child view. Parent is visible by default but the child view has to be expanded and collapsed. It will expand when we click on parent view.
This feature is quite similar to the ExpandableListView in Android. Since it is an old one, it does not perform well according to our requirement.

Expandable RecyclerView View Holders

We have to create two view holders to hold the view of parent and child

  • ParentViewHolder
  • ChildViewHolder.

So, by using these we shall create it in our application to make it better.


Add this dependency in your build.gradle file.

dependencies {
compile ''

After adding the library, we have to add some Java classes with the help of this library like Adapter, Model, and View holders.

Expandable RecyclerView Example with Code:

Now, it’s time to do it programmatically. So here in this example we will show Movie category as a parent and movies according to the category are a child.
Create an Application Expandable RecyclerView ExampleAndroid.

Requirements:,,,,,, activity_main.xml, movie_category_view.xml, movies_view.xml.
After adding a library and some Java classes let’s do it one by one in our project.

Create this parent class, implements ParentListItem and override its method

Add the code here

public class MovieCategory implements ParentListItem {
private List<Movies> mMovies;
public List<?> getChildItemList() {
return mMovies;

Here, it returns the list of movies.

Create this child class it is used to show the data in each child of a parent.

Add the code here

public class Movies {
private String mName;
public Movies(String name) {
mName = name;
public String getName() {
return mName;


Create this XML file for creating the view of movie category.
Add the code inside LinearLayout.

<TextView android:id="@+id/tv_movie_category"
<ImageView android:id="@+id/iv_arrow_expand"

It shows a text view with an arrow icon.

Create this class and extend it with ParentViewHolder. It is a class which binds the view. Here we take the reference of MovieCategory view and set the properties when it is expanded and collapsed.

Add the code here

mMovieTextView = (TextView) itemView.findViewById(;
mArrowExpandImageView = (ImageView) itemView.findViewById(;

Add the code in bind(…) method


Now, set the properties for expand and collapse of arrow image and its rotation accordingly by implementing two methods setExpanded() and onExpansionToggled().

Add the code here:

public void setExpanded(boolean expanded) {
if (expanded) {
} else {
public void onExpansionToggled(boolean expanded) {
RotateAnimation rotateAnimation;
if (expanded) { // rotate clockwise
rotateAnimation = new RotateAnimation(ROTATED_POSITION,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
} else { // rotate counterclockwise
rotateAnimation = new RotateAnimation(-1 * ROTATED_POSITION,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);


Create this XML file. It shows the view of child items.
Add the code inside LinearLayout.

<TextView android:id="@+id/tv_movies"

Here, it shows the name of movies with respect to the parent.

Create this class and extend it with ChildHolder. Here, we take the reference of the child view.

Add the code here

mMoviesTextView = (TextView) itemView.findViewById(;

Add the code in bind(..) method:

public void bind(Movies movies) {

Create this custom Adapter class which extends ExpandableRecyclerAdapter. Here, it has two different sets of onCreateViewHolder and onBindViewHolder. One of which is for parent and the other for the child.

Add the codes here

public MovieCategoryViewHolder onCreateParentViewHolder(ViewGroup parentViewGroup) {
View movieCategoryView = mInflator.inflate(R.layout.movie_category_view, parentViewGroup, false);
return new MovieCategoryViewHolder(movieCategoryView);
public MoviesViewHolder onCreateChildViewHolder(ViewGroup childViewGroup) {
View moviesView = mInflator.inflate(R.layout.movies_view, childViewGroup, false);
return new MoviesViewHolder(moviesView);
public void onBindParentViewHolder(MovieCategoryViewHolder, int position, ParentListItem parentListItem) {
MovieCategory = (MovieCategory) parentListItem;
public void onBindChildViewHolder(MoviesViewHolder, int position, Object childListItem) {
Movies movies = (Movies) childListItem;


Create this XML file for RecyclerView.
Add the code inside LinearLayout.


It is in our Main Activity that we pass the data, set the adapter and take the reference of Recycler View.

Add the code here


private MovieCategoryAdapter mAdapter;
private RecyclerView recyclerView;
Add the code in onCreate(..){}:
Movies movie_one = new Movies("The Shawshank Redemption");
Movies movie_two = new Movies("The Godfather");
Movies movie_three = new Movies("The Dark Knight");
Movies movie_four = new Movies("Schindler's List ");
MovieCategory molvie_category_one = new MovieCategory("Drama", Arrays.asList(movie_one, movie_two, movie_three, movie_four));

Like this, we can add more movie categories.

final List<MovieCategory> movieCategories = Arrays.asList(molvie_category_one, molvie_category_two, molvie_category_three,molvie_category_four);
recyclerView = (RecyclerView) findViewById(;
mAdapter = new MovieCategoryAdapter(this, movieCategories);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

We can also set the properties of expanding and collapse it by setExpandCollapseListener(..).

Add the code when our list item Expanded and collapsed:

mAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
public void onListItemExpanded(int position) {
MovieCategory expandedMovieCategory = movieCategories.get(position);
String toastMsg = getResources().getString(R.string.expanded, expandedMovieCategory.getName());
public void onListItemCollapsed(int position) {
MovieCategory collapsedMovieCategory = movieCategories.get(position);
String toastMsg = getResources().getString(R.string.collapsed, collapsedMovieCategory.getName());

Now, set the adapter and layout manager in our recycler view.

recyclerView.setLayoutManager(new LinearLayoutManager(this));

Here, we can do like this.

Save and Restore the state of Expand / Collapsed:

We can save the expanded and collapsed state of items in our Recycler View during our device configuration changes. Other things and also we can store the state of same.
To save Expanded / Collapsed state we can call ExpandableRecyclerAdapter.onSaveInstanceState(Bundle savedInstanceState).

protected void onSaveInstanceState(Bundle outState) {

Like this, we can save the state.
To restore expanded / collapsed state we can call ExpandableRecyclerAdapter.onRestoreInstanceState(Bundle savedInstanceState).

protected void onRestoreInstanceState(Bundle savedInstanceState) {

Like this, we can restore the state.

Output:Expandable RecyclerView Android

Download Code: Github


This was an interesting example of how to create Expandable RecyclerView by using Big Nerd Ranch’s library in our application. This feature is mainly used in many applications. We can customize it according to the requirement. It is the same as ExapandableListView but has some new features added. An interesting functionality of Android, it is frequently used by the developers.

Suggested Reading

Android ExpandableListview

Related Popular Courses:




  1. HEY thats nice tutorial but can u explain me how do i get the child position in the fragments class and how to make like only one to be expandable if i have 3 views i need only one among them to be opened first please help me put

    1. You have no need of reference of Fragment view to get its components in Activity. As you can directly access layout components of a Fragment in parent Activity.
      Simply you can access any component by this

  2. How can we optimize the multiple lines
    Movies movie_one = new Movies(“The Shawshank Redemption”);
    Movies movie_two = new Movies(“The Godfather”);
    Movies movie_three = new Movies(“The Dark Knight”);
    Movies movie_four = new Movies(“Schindler’s List “);
    into one line. May be by using list or array. Please suggest

    1. Hi Yaka,
      Thank you for asking us and reading our blogs..!!
      In this code, We are creating objects for all four Movies, so you cannot take it into single line. Add for all to assign in different Movies objects.

  3. Hi, it was a nice tutorial. I want to show toast message when clicked on any child item, lets say Shawshank redemption. Can you tell where should i put that code.

  4. I cannot use the function “notifyDataSetChanged()”. I tried to fill it with new records

    At first, I thought it was an issue when I apply retrofit, but NO because I created local records before initializing the adapter, it showed me the records, but after initializing it I created my local records and called the function “notifyDataSetChanged()” it didn’t show me the records.
    How can I solve it?

  5. can anybody tell me why i can’t use ParentListItem or ParentObject
    what was the reason that i can’t implement this classes. i’d added the dependency Big Nerd Ranch’s Expandable RecyclerView Library
    but still i can’t use these classes. any suggestion for what i missed. please help 🙁

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles