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

Android 開發系列(八):ExpandableListView 的基本用法

編輯:關於 Android 程式設計

前面寫過 ListView 的用法,並做了一個 QQ 通訊人列表。這次在同样的基礎上談 ExpandableListView——也就是可展開的分組列表。示範先從最省事的 SimpleExpandableListAdapter 開始,之後再談自訂 Adapter 的做法。

一、準備三個布局檔

與 ListView 類似,ExpandableListView 需要三份布局:主布局、一級項目(組)布局、二級項目(子項)布局。

main.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- 注意這裡的 id 使用系統自帶的 list -->
    <ExpandableListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

group_layout.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myGroup"
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:paddingLeft="50dip"
        android:textSize="12pt"
        android:gravity="center_vertical"/>
</LinearLayout>

child_layout.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myChild"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:paddingLeft="70dip"
        android:textSize="10pt"
        android:gravity="center_vertical"/>
</LinearLayout>

二、撰寫 MainActivity

這裡讓 MainActivity 直接繼承 ExpandableListActivity,就能用 setListAdapter() 設定適配器。

package com.example.android_expandablelistview;

import java.util.*;
import android.os.Bundle;
import android.app.ExpandableListActivity;
import android.widget.SimpleExpandableListAdapter;

public class MainActivity extends ExpandableListActivity {

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

        // 一級項目的資料
        String[] keysOfGroup = {"groupName"};
        String[] valuesOfGroup = {"group1", "group2"};
        ArrayList<HashMap<String, String>> listOfGroup =
                getDataList(keysOfGroup, valuesOfGroup);

        // 二級項目的資料:一級有幾組,就要準備幾份二級清單
        String[] keysOfChild = {"childName"};
        // 不同組別的子項目共用同一組鍵名
        String[] valuesOfChild1 = {"child1Data1", "child1Data2"};
        String[] valuesOfChild2 = {"child2Data1"};

        ArrayList<HashMap<String, String>> listOfChild1 =
                getDataList(keysOfChild, valuesOfChild1);
        ArrayList<HashMap<String, String>> listOfChild2 =
                getDataList(keysOfChild, valuesOfChild2);

        // 依 group 的順序,把每份二級清單裝進外層 List
        ArrayList<ArrayList<HashMap<String, String>>> childs =
                new ArrayList<ArrayList<HashMap<String, String>>>();
        childs.add(listOfChild1);
        childs.add(listOfChild2);

        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
                this,
                listOfGroup, R.layout.group_layout,
                keysOfGroup, new int[]{R.id.myGroup},
                childs, R.layout.child_layout,
                keysOfChild, new int[]{R.id.myChild});

        setListAdapter(adapter);
    }

    /**
     * 組出 List<Map<String, String>>
     *
     * @param keys   每個 Map 的鍵名
     * @param values 所有項目的值;由於每個項目對應多個鍵名,這裡用一維陣列依序排列
     */
    private ArrayList<HashMap<String, String>> getDataList(
            String[] keys, String[] values) {
        ArrayList<HashMap<String, String>> list =
                new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < values.length / keys.length; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            for (int j = 0; j < keys.length; j++) {
                map.put(keys[j], values[i * keys.length + j]);
            }
            list.add(map);
        }
        return list;
    }
}

三、幾個容易踩到的點

  • 主布局的 ExpandableListView 建議使用系統 id @android:id/list,因為 ExpandableListActivity 就是靠它來找控制項。
  • SimpleExpandableListAdapter 的 group 資料是「一組一筆 Map」,而 child 資料是「List 的 List」,外層順序必須和 group 的順序一一對應,錯位就會看到子項目掛錯組。
  • 不同 group 底下可以有不同數量的子項目,但鍵名必須一致,因為同一個 adapter 只接受一組 child keys。
  • paddingLeft 做出來的階梯感只是視覺效果;正式專案建議用 selector 或 shape 標示展開狀態,並處理好不同 dpi 下的間距。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved