Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 控制項:ExpandableListView 可展開清單

編輯:Android 開發實例

ExpandableListView 提供的是兩層結構的清單:一級群組標題可以展開或收合,底下再列出二級子項。像設定頁的分類選單、音樂 App 的「專輯 → 曲目」這類介面,都很適合用它。

版面配置:共需三個檔

實作時需要準備三份 XML,都放在 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 類似,特別的是 id 用了 @id/android:list 與 @id/android:empty 這兩個系統保留名稱,前者讓 ExpandableListActivity 能找到清單,後者則是資料為空時自動顯示的佔位文字。groups.xml 負責一級標題的外觀(字級 25sp,縮排較深),childs.xml 負責二級子項(字級 15sp,縮排較淺),透過縮排深度讓層級關係一眼可辨。

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.view.View;
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 顯示資料
         * 參數 1:Context 物件
         * 參數 2:一級項目集合
         * 參數 3:一級項目對應的版面檔
         * 參數 4:from,指定要顯示的 Map key
         * 參數 5:to,與 from 對應,指定要填入的 View id
         * 參數 6:二級項目集合
         * 參數 7:二級項目對應的版面檔
         * 參數 8:from,同上
         * 參數 9:to,同上
         */
        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) {
        // do something
        return super.setSelectedChild(groupPosition, childPosition, shouldExpandGroup);
    }

    /** 設定預設選中的一級項目 */
    @Override
    public void setSelectedGroup(int groupPosition) {
        // do something
        super.setSelectedGroup(groupPosition);
    }

    /** 二級項目被點擊時的回呼 */
    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
                                int groupPosition, int childPosition, long id) {
        // do something
        return super.onChildClick(parent, v, groupPosition, childPosition, id);
    }
}

幾個實作重點

SimpleExpandableListAdapter 的建構子參數確實很多,但只要記住它是「一組、二組」成對出現的就不容易搞混:前三個參數描述一級項目,後四個描述二級項目,中間用 from/to 的對應關係把資料填入版面。

資料結構上要注意層級:groups 是一層 List,childs 則是「List 的 List」,外層對應每個群組,內層對應該群組底下的子項,兩者的順序必須一一對齊。

最後三個覆寫的方法(setSelectedGroup、setSelectedChild、onChildClick)都是在你需要客製行為時才動手,平時直接呼叫 super 即可。實際要放進哪些邏輯,取決於你的需求。

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