Android


Expandable list view in android example

Expandable list view in android

This is a wonderful example to show you, how to create an expandable list view in android. I have learned already in the previous article, how to create a list view in android by Recyclerview.

So now you can use the ExpandableListView component to create a list of parent and child relationship. This is an awesome component to convert your JSON/XML data to represent with a respective manner.

Methods of Expandable list view

Expandable List view in android loads the data into the items associated with this view. These are some important methods in this class:

  • setChildIndicator(Drawable)
  • setGroupIndicator(Drawable)
  • getGroupView()
  • getChildView()

setChildIndicator(Drawable): This is used to show an indicator beside each item representing the current state. If the child is the last child for a group, the state state_last will be set.

setGroupIndicator(Drawable): An indicator is drawn beside the group representing its state i.e. expanded or collapsed. If the group is empty. The state state_empty will be set. If the group is expanded, the state state_expanded will be set.

getGroupView(): It returns a view for the list group header.

getChildView(): It returns view for list child item.

The expandable view that shows items in a vertically scrolling two-level list. Expandable lists are able to show an indicator beside each item to display the item’s current state.

The states are usually one of an expanded group, collapsed group, child, or last child.

All pages of project

Create your main layout with the help of tab layout and toolbar options. 

activity_main.xml

fragment_data_list.xml

In the adapter_list_group layout file – you can create your design. In this example, I have used the card view for the good looking header section. 

adapter_list_group.xml

adapter_list_item.xml

Handle all ClickListeners from your DataFragment.java class. Basically, four types of ClickListeners are there:

  • ExpandableListView.OnChildClickListener
  • ExpandableListView.OnGroupClickListener
  • ExpandableListView.OnGroupCollapseListener
  • ExpandableListView.OnGroupExpandListener

ExpandableListView.OnChildClickListener: You can handle ChildClickListener, This is overridden to implement the callback method that’s invoked when a child in the expanded list is clicked

ExpandableListView.OnGroupClickListener: This is overridden to implement the callback method that’s invoked when a group header in the expanded list is clicked.

ExpandableListView.OnGroupCollapseListener: This is used for notifying when a group is collapsed.

ExpandableListView.OnGroupExpandListener: This is used to notify when a group is expanded.

DataFragment.java

TabPagerAdapter.java

ExpandableListAdapter.java

MainActivity.java

In the Gradle file, you need to add one more line:

build.gradle

AndroidManifest.xml

Must Read:

How to display an animated image in the android

Published by Amit Pandey

Professional Android app Developer, Full Stack Web Developer, Blogger, Entrepreneur And Founder of Coding Issue. I am always trying to learn new things or new possible ways to do things.

Did you find this page helpful?

X

Leave a Reply

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

*

*

*