Android Expandable ListView Tutorial with Example

By | June 7, 2019

Expandable ListView in Android Tutorial Example

What is Expandable ListView ?

Expandable ListView is a View that shows items in a vertically scrolling two level list. ListView allowed two level groups which can individually be expanded to show its children. Each group can be expanded or collapsed individually to show or hide its children items.

Expandable ListView is used to group data by Categories wise. Expandable ListView is a group of the child items. When I click on any group to expanded and show the child items again click on the group it will collapse, we can also do different actions on child items to make it custom.

Expandable ListView is used to BaseExpandableListAdapter adapter.

Expandable ListView Adapter Methods

getGroupCount() : This Method is used to get the number of groups.

getChildrenCount(int groupPosition) : This Method is used to gets the number of children in a specified group.

getGroup(int groupPosition) : This Method is used to  gets the data associated with the given group.

getChild(int groupPosition, int childPosition) : This Method is used to gets the data associated with the given child within the given group.

getGroupId(int groupPosition) : This Method is used to get the ID for the group at the given position.

 getChildId(int groupPosition, int childPosition) : This Method is used to gets the ID for the given child within the given group.

hasStableIds() : This Method is used to Indicates the child and group ID’s are stable across changes to the data.

getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) : This Method is used to Create our group or parent View .

How to Create Expandable ListView Example ?

Create activity_main.xml File Follow this code.

Create parent_item.xml File Following This code.

Create child_item.xml File Following This Code.

Create MainActivity.java File Follow this code.

Create MyAdapter.java File Follow this code.

Above the example of i have create expandable listView and create parent and child layout xml file.Now this file bind of adapter and then user to click on parent item to display child item and then re click to hide of child item.

Now In this child layout to visible or hide operation performed. first time is hide layout in child.when you can click parent layout to display child layout and then display child layout textView.