Last Updated:

ExpandableListView in Android | Examples

A quick tour of ExpandableListView in Android

A two-level list is a view object in which each list item contains a nested list. An adapter is responsible for the operation of the list, which loads data for each item and sublist.ExpandableListViewExpandableListViewAdapter

Let's look at the most useful methods that are used when working with a class:ExpandableListView

  • The method is used to display an indicator next to each list item.setChildIndicator(Drawable)
  • The method sets the indicator near each element, indicating whether it is expanded or collapsed. If the group is empty, the status will be set to . If the group is deployed, the status will be set to .setGroupIndicator(Drawable)state_emptystate_expanded
  • The method returns a view for a group of list itemsgetGroupView()
  • The method returns the view of a child item in the listgetChildView()

Each element uses a different listener type:

  • The interface is overridden to track clicks on child items in the drop-down list ExpandableListView.OnChildClickListener
  • The interface is overridden to track clicks on a group of items ExpandableListView.OnGroupClickListener
  • The interface is used to notify you when a group of items has been collapsed ExpandableListView.OnGroupCollapseListener
  • The interface is used to notify you when a group of items has been expanded ExpandableListView.OnGroupExpandListener

An example of creating an ExpandableListView. Practice

Only 3 classes will be used in our application:

  1. MainActivity class is the main activity in which the layout with ExpandableListView will be
  2. The ListData class is the class that will store the data for the view in the ExpandableListView. In real projects, the data source will of course be the database or some data from the network.
  3. The ListAdapter class is a class that is responsible for managing data from ListData in the .ExpandableListView

The activity_main.xml layout file will look like this:

Pay attention to the use of the attribute - it sets the list indicator (whether it is open or not) to the left of the item name.android:indicatorLeft

We'll also need to list_group.xml layout for the child elements of each item's drop-down list:

The layout of one child element is as follows:

Now let's imagine the ListData class, which will store the data of the entire list:ExpandableListView

As you can see, our HashMap contains as keys the names of the parent elements of the drop-down list, and as the values - a list of child elements (those elements that will be shown when you click on the drop-down parent element of the list).

We define in the ListAdapter class which elements will be shown in , and which in and how our adapter will work:GroupViewChildView

Well, we collect it all in the main MainActivity class, which will display our :ExpandableListView

As you can see in the code above, we implemented the interfaces described at the beginning of the article and showed the element that the user clicked on in the Toast pop-up window.

The result of creation in Android:ExpandableListView

Stay tuned and subscribe to new articles!