Wednesday, January 4, 2017

Easy Tutorial Android Expandable RecyclerView

Today i going to share a awesome tutorial using recyclerview. As usual, you may use ListView for creating a collapse and expand view, right?

We using listview with reason that listview have a view called ExpandableListView, so we just set the data through ExapandableListView.

But in several case, your application required a Recyclerview (I don't know what it is) instead of Listview.

Indeed, create a expandable view using recyclerview is more difficult that using ExpandableListview. You have to create all materials or view not just set the data.

But don't worry, i have spent several hours just to create simple expandable view using recyclerview, with hope you will easy to understand the steps.


Compile the following libraries :

compile ''
compile ''
compile ''

After that, download LIB MODULE EXPANDABLE , extract and then place in your main package. Confuse? Take a look at Figure 1 :

Figure 1

Place the LIB MODULE EXPANDABLE look like figure 1.


Create new 3 xml layouts :
  1. activity_main.xml
  2. item_list_parent.xml
  3. item_list_child.xml

At Figure 1, there are several packages that have some java classes.

Package models

Package expandables
And last, definitely

First, modify the layouts that we've prepared.

activity_main.xml. Paste the following code :

item_list_child.xml. Paste the following code :

item_list_parent.xml. Paste the following code :

Second, we modify the code of all the java classes inside the package models. Why this package is first? Because this classes will use in next next java classes.

1. Package MODELS

Open This class store all the data that will show on expand view. Paste the following code :

Then modify class This class contains data from class BiodataChildModel and set as a list.

Because this class as the parent, you have to extends ParentListItem. After you do extends, you have to implements its methods. Take a look at the following code  :

Attention :
Make sure you have returned mListChild in method getChildItemList()


Open class Paste the following code :

Open class Paste the following code :

In code above there are method :
  1. setExpanded(boolean expanded) 
  2. onExpansionToggled(boolean expanded) . 
Those method used to create an animation of the imageview (icon + and -) when the view is expand or collapse. You can see on the video demo above.

Open class Paste the following code :

All the class that used to create expandable has done. Time to input the data and set adapter in your Paste the following code :

Done!!! Run your project, hope the project is running well :D. Thank you.

Download the project here

Related Posts