https://www.journaldev.com/9942/android-expandablelistview-example-tutorial

Welcome to Android ExpandableListView Example Tutorial. In this tutorial we’ll implement an ExpandableListView which is used to group list data by categories. It’s sort of menu and submenus in a Android ListView.

Android ExpandableListView

Android ExpandableListView is a view that shows items in a vertically scrolling two-level list. It differs from a ListView by allowing two levels which are groups that can be easily expanded and collapsed by touching to view and their respective children items.
ExpandableListViewAdapter in android loads the data into the items associated with this view.

Following are some important methods that are used by this class :

 
  • setChildIndicator(Drawable)
    : This is used to show an indicator besides 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 besides 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 view for the list group header
  • getChildView() : It returns view for list child item

The notable interfaces that are implemented by this class are given below :

  • ExpandableListView.OnChildClickListener : 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 : It is used for notifying when a group is collapsed
  • ExpandableListView.OnGroupExpandListener : It is used to notify when a group is expanded

Android ExpandableListView Project Structure

This project consists of three classes.

  • A MainActivity that shows the layout with the ExpandableListView
  • An ExpandableListDataPump which represents a random data in a List and maps the child item data to the respective group headers using a HashMap
  • A CustomExpandableListAdapter which provides the MainActivity with the data from the ExpandableListDataPump class/li>

Android ExpandableList

The activity_main.xml layout consists of an ExpandableListView in a RelativeLayout as shown below :

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity">
  10.  
  11. <ExpandableListView
  12. android:id="@+id/expandableListView"
  13. android:layout_height="match_parent"
  14. android:layout_width="match_parent"
  15. android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
  16. android:divider="@android:color/darker_gray"
  17. android:dividerHeight="0.5dp" />
  18.  
  19. </RelativeLayout>

The android:indicatorLeft is the left bound for an items indicator.

 

Note : We cannot use the value wrap_content for the android:layout_height attribute of the ExpandableListView in XML unless the parent’s size is strictly specified

The layout of the group header of each individual list is given below :

list_group.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical" android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TextView
  7. android:id="@+id/listTitle"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
  11. android:textColor="@android:color/black"
  12. android:paddingTop="10dp"
  13. android:paddingBottom="10dp" />
  14. </LinearLayout>

The layout row of the child items is given below :

list_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical" android:layout_width="match_parent"
  5. android:layout_height="wrap_content">
  6. <TextView
  7. android:id="@+id/expandedListItem"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
  11. android:paddingTop="10dp"
  12. android:paddingBottom="10dp" />
  13. </LinearLayout>

The ExpandableListDataPump class is defined as below:

  1. package com.journaldev.expandablelistview;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6.  
  7. public class ExpandableListDataPump {
  8. public static HashMap<String, List<String>> getData() {
  9. HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
  10.  
  11. List<String> cricket = new ArrayList<String>();
  12. cricket.add("India");
  13. cricket.add("Pakistan");
  14. cricket.add("Australia");
  15. cricket.add("England");
  16. cricket.add("South Africa");
  17.  
  18. List<String> football = new ArrayList<String>();
  19. football.add("Brazil");
  20. football.add("Spain");
  21. football.add("Germany");
  22. football.add("Netherlands");
  23. football.add("Italy");
  24.  
  25. List<String> basketball = new ArrayList<String>();
  26. basketball.add("United States");
  27. basketball.add("Spain");
  28. basketball.add("Argentina");
  29. basketball.add("France");
  30. basketball.add("Russia");
  31.  
  32. expandableListDetail.put("CRICKET TEAMS", cricket);
  33. expandableListDetail.put("FOOTBALL TEAMS", football);
  34. expandableListDetail.put("BASKETBALL TEAMS", basketball);
  35. return expandableListDetail;
  36. }
  37. }

In the above code the expandableListDetail object is used to map the group header strings to their respective children using an ArrayList of Strings.

 

CustomExpandableListAdapter.java

  1. package com.journaldev.expandablelistview;
  2.  
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import android.content.Context;
  6. import android.graphics.Typeface;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.BaseExpandableListAdapter;
  11. import android.widget.TextView;
  12.  
  13. public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
  14.  
  15. private Context context;
  16. private List<String> expandableListTitle;
  17. private HashMap<String, List<String>> expandableListDetail;
  18.  
  19. public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
  20. HashMap<String, List<String>> expandableListDetail) {
  21. this.context = context;
  22. this.expandableListTitle = expandableListTitle;
  23. this.expandableListDetail = expandableListDetail;
  24. }
  25.  
  26. @Override
  27. public Object getChild(int listPosition, int expandedListPosition) {
  28. return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
  29. .get(expandedListPosition);
  30. }
  31.  
  32. @Override
  33. public long getChildId(int listPosition, int expandedListPosition) {
  34. return expandedListPosition;
  35. }
  36.  
  37. @Override
  38. public View getChildView(int listPosition, final int expandedListPosition,
  39. boolean isLastChild, View convertView, ViewGroup parent) {
  40. final String expandedListText = (String) getChild(listPosition, expandedListPosition);
  41. if (convertView == null) {
  42. LayoutInflater layoutInflater = (LayoutInflater) this.context
  43. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  44. convertView = layoutInflater.inflate(R.layout.list_item, null);
  45. }
  46. TextView expandedListTextView = (TextView) convertView
  47. .findViewById(R.id.expandedListItem);
  48. expandedListTextView.setText(expandedListText);
  49. return convertView;
  50. }
  51.  
  52. @Override
  53. public int getChildrenCount(int listPosition) {
  54. return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
  55. .size();
  56. }
  57.  
  58. @Override
  59. public Object getGroup(int listPosition) {
  60. return this.expandableListTitle.get(listPosition);
  61. }
  62.  
  63. @Override
  64. public int getGroupCount() {
  65. return this.expandableListTitle.size();
  66. }
  67.  
  68. @Override
  69. public long getGroupId(int listPosition) {
  70. return listPosition;
  71. }
  72.  
  73. @Override
  74. public View getGroupView(int listPosition, boolean isExpanded,
  75. View convertView, ViewGroup parent) {
  76. String listTitle = (String) getGroup(listPosition);
  77. if (convertView == null) {
  78. LayoutInflater layoutInflater = (LayoutInflater) this.context.
  79. getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  80. convertView = layoutInflater.inflate(R.layout.list_group, null);
  81. }
  82. TextView listTitleTextView = (TextView) convertView
  83. .findViewById(R.id.listTitle);
  84. listTitleTextView.setTypeface(null, Typeface.BOLD);
  85. listTitleTextView.setText(listTitle);
  86. return convertView;
  87. }
  88.  
  89. @Override
  90. public boolean hasStableIds() {
  91. return false;
  92. }
  93.  
  94. @Override
  95. public boolean isChildSelectable(int listPosition, int expandedListPosition) {
  96. return true;
  97. }
  98. }

This class extends BaseExpandableListAdapter and it overrides the methods in the base class to provide the view for the ExpandableListView. getView() fills in the data into the item’s view with the given index.

MainActivity.java

  1. package com.journaldev.expandablelistview;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.ExpandableListAdapter;
  7. import android.widget.ExpandableListView;
  8. import android.widget.Toast;
  9.  
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.List;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.  
  16. ExpandableListView expandableListView;
  17. ExpandableListAdapter expandableListAdapter;
  18. List<String> expandableListTitle;
  19. HashMap<String, List<String>> expandableListDetail;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
  26. expandableListDetail = ExpandableListDataPump.getData();
  27. expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
  28. expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
  29. expandableListView.setAdapter(expandableListAdapter);
  30. expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
  31.  
  32. @Override
  33. public void onGroupExpand(int groupPosition) {
  34. Toast.makeText(getApplicationContext(),
  35. expandableListTitle.get(groupPosition) + " List Expanded.",
  36. Toast.LENGTH_SHORT).show();
  37. }
  38. });
  39.  
  40. expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
  41.  
  42. @Override
  43. public void onGroupCollapse(int groupPosition) {
  44. Toast.makeText(getApplicationContext(),
  45. expandableListTitle.get(groupPosition) + " List Collapsed.",
  46. Toast.LENGTH_SHORT).show();
  47.  
  48. }
  49. });
  50.  
  51. expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  52. @Override
  53. public boolean onChildClick(ExpandableListView parent, View v,
  54. int groupPosition, int childPosition, long id) {
  55. Toast.makeText(
  56. getApplicationContext(),
  57. expandableListTitle.get(groupPosition)
  58. + " -> "
  59. + expandableListDetail.get(
  60. expandableListTitle.get(groupPosition)).get(
  61. childPosition), Toast.LENGTH_SHORT
  62. ).show();
  63. return false;
  64. }
  65. });
  66. }
  67.  
  68. }

In the above code we’ve implemented all the interfaces that were discussed before. For the sake of simplicity, we’ll only display a Toast with the name of the item or the state of the group for every click. But these can be easily modified to perform any other operations.

Below is our app with android expandable list view in action.

Note: ExpandableListViews are scrollable by default.

This brings an end to Android ExpandableListView tutorial. You can download the final Android ExpandableListView Project from the below link.

android 的 ExpandableListView Example Tutorial的更多相关文章

  1. 22.Android之ExpandableListView树形列表学习

    Android经常用到树形菜单,一般ExpandableListView可以满足这个需要,今天学习下. XML代码: <?xml version="1.0" encoding ...

  2. Android之ExpandableListView

    ExpandableListView可以用来表现多层级的listView,本文主要是ExpandableListView的一个简单实现 布局文件 <LinearLayout xmlns:andr ...

  3. Android 之 ExpandableListView 的使用

    喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到an ...

  4. Android中ExpandableListView控件基本使用

    本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...

  5. Android在ExpandableListView控制的基本使用

    在本文中,Demo为了展示Android在ExpandableListView用途管制.如该组/儿子ListView绑定数据源. 直接上代码例如以下: 程序结构图: layout文件夹下的 main. ...

  6. android原生ExpandableListView

    android原生可扩展ExpandableListView就是可以伸缩的listView,一条标题下面有多条内容. 这个list的adapter对的数据要求与普通ListView的数据要求也有一些差 ...

  7. Android中ExpandableListView的使用

    ExpandableListView是Android中可以实现下拉list的一个控件,具体的实现方法如下: 首先:在layout的xml文件中定义一个ExpandableListView < L ...

  8. android 之 ExpandableListView列表中的列表

    有时候,我们需要设计这样一个界面,外面有一个列表,当我们点击其中列表中的某个条目时,就会展开这个条目,出现一个新的列表.比如下图:(程序运行的效果图,在这里贴出来) 当我们点击第一项时,视图变为: - ...

  9. Android 关于ExpandableListView去掉里头的分割线

    关于ExpandableListView去掉里面的分割线关于ExpandableListView,自己写了个类继承自BaseExpandableListAdaptergroups,childs都弄好了 ...

随机推荐

  1. Dll 使用 PChar 参数的小例子 - 回复 "linximf" 的问题

    本例效果图: Dll 文件: library Lib; uses   SysUtils, Classes; {$R *.res} procedure Test(p: PChar); const   T ...

  2. Strut2------源码下载

    转载: http://download.csdn.net/detail/dingkui/6858009

  3. VirtualBox设置NAT端口映射

    原文地址 :http://www.2cto.com/os/201209/153863.html   VirtualBox设置NAT端口映射   好吧,我知道这个问题有很多人都讲过,但是,你们不觉得VB ...

  4. RF常用快捷键

    转自:http://www.robotframework.net/article/47 重命名——>F2 搜索关键字——>F5 执行用例——>F8 创建新工程——>ctrl+n ...

  5. EventBus 简单原理(一)

    EventBus 1.根据文章最前面所讲的EventBus使用步骤,首先我们需要定义一个消息事件类: public class MessageEvent { private String messag ...

  6. system times on machines may be out of sync

    今天在hadoop集群执行任务的时候报了一个这个错误,听名字应该是三台机器的时间不同步.于是同步一下时间即可解决 1.安装ntpdate工具 yum -y install ntp ntpdate 2. ...

  7. Cognos入门教程

    Cognos入门教程 1. ReportStudio入门教程 ReportStudio入门教程(http://blog.csdn.net/column/details/ygy-reportstudio ...

  8. Java的String详解

    Java的String详解 博客分类: Java javaStringString详解常用方法  Java的String类在开发时经常都会被使用到,由此可见String的重要性.经过这次认真仔细的学习 ...

  9. python初学总结(二)

    (1)字典 字典是一种映射关系:键(key),值(value),key-value对 创建字典的方式:直接创建和利用dict函数创建 >>> aInfo = {'Wangdachui ...

  10. struts2漏洞原理

    一.struts2简介: 目前web框架中非常流行的都是mvc设计模式.经典例子例如:python的Django.Flask:java的ssm等.因为使用MVC设计模式,所以在框架内部处理用户数据流参 ...