本節主要講解可展開列表組件 ExpandableListView。它的配置有些繁瑣,也容易出問題,因此本文的範例盡量拿掉了干擾內容,讓大家能更清晰地理解、也更容易借鏡。
先給大家演示程式運行結果。

點擊一級列表,展開下一級:

點擊二層列表(嵌套的列表)的某一項:

下面我們來看程式碼:
1、新建一個專案 Lesson43_ExpandableListView。
2、main.xml 的內容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android">
- <ExpandableListView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@id/android:list">
- </ExpandableListView>
- </LinearLayout>
請注意 ExpandableListView 標籤中 id 的寫法是固定的 @id/android:list,因為我們這裡用的是 ExpandableListActivity,而它的程式碼裡寫死了要尋找的 UI 元素就是這個,這和我們以前講過的許多特殊 Activity 一樣,這裡再稍作提醒。
3、MainActivity.java 的程式碼如下,說明都寫在註解裡:
- package basic.android.lesson43;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.ExpandableListActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ExpandableListView;
- import android.widget.SimpleExpandableListAdapter;
- import android.widget.Toast;
- public class MainActivity extends ExpandableListActivity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 準備頂層列表數據
- List<Map<String, String>> topList = new ArrayList<Map<String, String>>();
- Map<String, String> topMap1 = new HashMap<String, String>();
- Map<String, String> topMap2 = new HashMap<String, String>();
- topMap1.put("month", "三月測評項");
- topMap2.put("month", "四月測評項");
- topList.add(topMap1);
- topList.add(topMap2);
- // 準備二層列表數據
- List<List<Map<String, String>>> nestList = new ArrayList<List<Map<String, String>>>();
- // 準備二層列表第一個子列表數據
- List<Map<String, String>> nestList1 = new ArrayList<Map<String, String>>();
- Map<String, String> nestMap1 = new HashMap<String, String>();
- Map<String, String> nestMap2 = new HashMap<String, String>();
- Map<String, String> nestMap3 = new HashMap<String, String>();
- nestMap1.put("test", "看手");
- nestMap2.put("test", "吃手");
- nestMap3.put("test", "玩手");
- nestList1.add(nestMap1);
- nestList1.add(nestMap2);
- nestList1.add(nestMap3);
- // 準備二層列表第二個子列表數據
- List<Map<String, String>> nestList2 = new ArrayList<Map<String, String>>();
- Map<String, String> nestMap4 = new HashMap<String, String>();
- Map<String, String> nestMap5 = new HashMap<String, String>();
- nestMap4.put("test", "翻身");
- nestMap5.put("test", "辨別聲音來源方位");
- nestList2.add(nestMap4);
- nestList2.add(nestMap5);
- // 把子列表數據放入
- nestList.add(nestList1);
- nestList.add(nestList2);
- // 準備數據匹配器
- SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
- this, // 1.上下文
- topList, // 2.頂層數據列表
- android.R.layout.simple_expandable_list_item_1, // 3.一層顯示樣式
- new String[]{"month"}, // 4.頂層map的鍵
- new int[]{android.R.id.text1}, // 5.頂層數據顯示的View ID
- nestList, // 6.二層數據列表
- android.R.layout.simple_list_item_1, // 7.二層顯示樣式
- new String[]{"test"}, // 8.二層map的鍵
- new int[]{android.R.id.text1} // 9.二層數據顯示的View ID
- );
- // 設定數據匹配器
- this.setListAdapter(adapter);
- }
- @Override
- public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
- Toast.makeText(this, "嵌套列表被點擊,頂層列表定位" + groupPosition + "二層列表定位" + childPosition, Toast.LENGTH_LONG).show();
- return super.onChildClick(parent, v, groupPosition, childPosition, id);
- }
- @Override
- public void onGroupCollapse(int groupPosition) {
- Toast.makeText(this, "頂層列表收縮,列表定位" + groupPosition, Toast.LENGTH_LONG).show();
- super.onGroupCollapse(groupPosition);
- }
- @Override
- public void onGroupExpand(int groupPosition) {
- Toast.makeText(this, "頂層列表展開,列表定位" + groupPosition, Toast.LENGTH_LONG).show();
- super.onGroupExpand(groupPosition);
- }
- }
4、編譯並運行程式,就能看到上面的效果。本節就上到這裡。Android 裡有不少內容就像本節的 ExpandableListView 一樣麻煩,來,我們一起吐槽一下它吧。