Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 控制項:ExpandableListView 兩級展開清單完整實作

編輯:關於 Android 開發

ExpandableListView 適合呈現「分類 → 細項」這種兩層結構,例如音樂 App 的分類與歌曲清單。實作時需要準備三個版面檔與一個 Adapter。

一、版面檔

三個版面檔都放在 res/layout 下,分別負責主介面、一級項目、二級項目。

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ExpandableListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@id/android:empty"
        android:text="No Data"/>
</LinearLayout>

groups.xml(一級項目):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/group"
        android:textSize="25sp"
        android:paddingLeft="35px"
        android:paddingTop="10px"
        android:paddingRight="5px"
        android:paddingBottom="10px"
        android:text="No Data"/>
</LinearLayout>

childs.xml(二級項目):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/child"
        android:textSize="15sp"
        android:paddingLeft="25px"
        android:paddingTop="10px"
        android:paddingRight="5px"
        android:paddingBottom="10px"
        android:text="No Data"/>
</LinearLayout>

三者的分工:main.xml 在主介面定義一個 ExpandableListView,這部分與做 ListView 時類似;groups.xml 定義一級條目的外觀(此處只放一個 TextView);childs.xml 定義二級條目的外觀(同樣只有一個 TextView,但字級較小、縮排較淺,用視覺層次區分兩層)。

二、Java 程式碼

package cn.yj3g.ExpandableListActivity;

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.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

/**
 * 繼承 ExpandableListActivity 類
 */
public class ExpandableActivity extends ExpandableListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // 創建一級條目
        List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
        Map<String, String> group1 = new HashMap<String, String>();
        group1.put("group", "音樂");
        Map<String, String> group2 = new HashMap<String, String>();
        group2.put("group", "歌詞");
        groups.add(group1);
        groups.add(group2);

        // 創建一級條目下的二級條目
        List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
        Map<String, String> childdata1 = new HashMap<String, String>();
        childdata1.put("child", "青花瓷");
        Map<String, String> childdata2 = new HashMap<String, String>();
        childdata2.put("child", "東風破");
        child1.add(childdata1);
        child1.add(childdata2);

        List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
        Map<String, String> childdata3 = new HashMap<String, String>();
        childdata3.put("child", "青花瓷.lrc");
        Map<String, String> childdata4 = new HashMap<String, String>();
        childdata4.put("child", "東風破.lrc");
        child2.add(childdata3);
        child2.add(childdata4);

        // 將二級條目放在一個集合裡,供顯示時使用
        List<List<Map<String, String>>> childs =
                new ArrayList<List<Map<String, String>>>();
        childs.add(child1);
        childs.add(child2);

        /**
         * 使用 SimpleExpandableListAdapter 顯示 ExpandableListView
         * 參數1.上下文物件 Context
         * 參數2.一級條目目錄集合
         * 參數3.一級條目對應的版面檔
         * 參數4.from,就是 map 中的 key,指定要顯示的物件
         * 參數5.與參數4對應,指定要顯示在 groups 中的 id
         * 參數6.二級條目目錄集合
         * 參數7.二級條目對應的版面檔
         * 參數8.from,就是 map 中的 key,指定要顯示的物件
         * 參數9.與參數8對應,指定要顯示在 childs 中的 id
         */
        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
                this, groups, R.layout.groups, new String[] { "group" },
                new int[] { R.id.group }, childs, R.layout.child,
                new String[] { "child" }, new int[] { R.id.child });
        setListAdapter(adapter);
    }

    /**
     * 設置哪個二級目錄被默認選中
     */
    @Override
    public boolean setSelectedChild(int groupPosition, int childPosition,
                                    boolean shouldExpandGroup) {
        return super.setSelectedChild(groupPosition, childPosition, shouldExpandGroup);
    }

    /**
     * 設置哪個一級目錄被默認選中
     */
    @Override
    public void setSelectedGroup(int groupPosition) {
        super.setSelectedGroup(groupPosition);
    }

    /**
     * 當二級條目被點擊時響應
     */
    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
                                int groupPosition, int childPosition, long id) {
        return super.onChildClick(parent, v, groupPosition, childPosition, id);
    }
}

三、要點提醒

SimpleExpandableListAdapter 的參數確實多,但規律很清楚。它其實是「兩組 SimpleListAdapter 的參數」並列:group 那一組是 (資料集合, 版面檔, from 陣列, to 陣列),child 那一組結構完全一樣。看懂這個對稱性,參數就不用死背。

資料結構必須是 List 裡包 List。childs 的型別是 List<List<Map<String,String>>>,外層清單的每一項對應一個一級條目,內層清單則是該條目底下的所有二級項目。順序必須與 groups 一一對應,否則會出現「音樂分類底下卻是歌詞檔」這種錯位。

三個重寫方法是擴充點。setSelectedGroup、setSelectedChild、onChildClick 分別對應預設選中一級、預設選中二級、點擊二級項目。範例中都只是轉呼叫父類別實作,實際使用時依需求替換成自己的邏輯即可——例如在 onChildClick 中播放被點選的歌曲。

注意版面檔的路徑。範例中 R.layout.child 少了 s(檔案名為 childs.xml),這是原程式碼的筆誤,照抄會編譯失敗,實作時請對齊為 R.layout.childs。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved