使用ExpandableListView和ExpandableListAdapter实现分组列表
ExpandableListView直接继承自ListView,而ExpandableListAdapter是一个超级接口,独立于Adapter接口。
ExpandableListAdapter类图:
下面的例子将ExpandableListView与SimpleExpandableListAdapter结合,实现分组列表。
先准备好三个布局文件:
主布局文件expandablelistview_activity.xml,显示ExpandableListView:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ExpandableListView
- android:id="@+id/expandableListView1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- </ExpandableListView>
- </LinearLayout>
父列表项布局文件expandable_group.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/expandable_group_textview"
- android:layout_marginLeft="30dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </LinearLayout>
子列表项布局文件expandable_child.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/expandable_child_textview"
- android:layout_marginLeft="50dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
Activity类:
- package com.zzj.ui.expandablelistviewdemo;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ExpandableListView;
- import android.widget.ExpandableListView.OnChildClickListener;
- import android.widget.SimpleExpandableListAdapter;
- import android.widget.Toast;
- import com.zzj.ui.R;
- public class MainActivity extends Activity implements OnChildClickListener {
- private String[] names = { "腾讯", "百度", "阿里巴巴" };
- private String[][] childnames = { { "QQ", "微信", "手机卫士" },
- { "百度地图", "百度视频", "PPS&奇艺" }, { "支付宝", "新浪微博", "高德地图" } };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.expandablelistview_activity);
- ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
- List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
- List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
- for (int i = 0; i < names.length; i++) {
- Map<String, String> map = new HashMap<String, String>();
- map.put("name", names[i]);
- groupData.add(map);
- String[] childs = childnames[i];
- List<Map<String, String>> list = new ArrayList<Map<String, String>>();
- for (int j = 0; j < childs.length; j++) {
- Map<String, String> childMap = new HashMap<String, String>();
- childMap.put("childname", childs[j]);
- list.add(childMap);
- }
- childData.add(list);
- }
- SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
- this, groupData, R.layout.expandable_group,
- new String[] { "name" },
- new int[] { R.id.expandable_group_textview }, childData,
- R.layout.expandable_child, new String[] { "childname" },
- new int[] { R.id.expandable_child_textview });
- expandableListView.setAdapter(adapter);
- expandableListView.setOnChildClickListener(this);
- }
- @Override
- public boolean onChildClick(ExpandableListView parent, View v,
- int groupPosition, int childPosition, long id) {
- String text = names[groupPosition] + "\r\n"
- + childnames[groupPosition][childPosition];
- Toast.makeText(this, text, Toast.LENGTH_LONG).show();
- return true;
- }
- }
父列表数据与子列表数据的顺序必须相对应。
给ExpandableListView设置adapter的时候调用的是setAdapter(ExpandableListAdapter adapter)方法,而不是setAdapter(Adapter adapter)方法。setAdapter(Adapter adapter)方法已被重写:
- @Override
- public void setAdapter(ListAdapter adapter) {
- throw new RuntimeException(
- "For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of " +
- "setAdapter(ListAdapter)");
- }
该方法已不允许被调用。
以上例子使用OnChildClickListener对子列表项的点击事件进行监听。
效果图如下:
可以参照SimpleExpandableListAdapter,扩展BaseExpandableListAdapter,实现自己的ExpandableListAdapter。

浙公网安备 33010602011771号