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

Android 二級展開列表:ExpandableListView 實作詳解

編輯:關於 Android 開發

打造「列表中的列表」

有時候我們需要這樣的界面:外層是一個列表,點擊某個條目後會展開,出現內嵌的子列表。這正是 ExpandableListView 的用武之地。下圖為程式運行後的效果:

點擊第一項後,視圖展開為:


需要準備的三個佈局

要實現這種效果,需要定義三個佈局:主佈局 activity_main、一級列表項 group、二級列表項 child。

activity_main:注意 ExpandableListView 的 id 必須為 @android:id/list,空資料提示的 TextView id 必須為 @android:id/empty,否則會出錯或失效。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.expandlistaacitvity.MainActivity" >

    <ExpandableListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawSelectorOnTop="false" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0000"
        android:text="no data" />
</LinearLayout>

group.xml(一級列表):

<?xml version="1.0" encoding="utf-8"?>
<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/groupTo"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="#0000ff"
        android:paddingTop="20dp"
        android:paddingLeft="60dp"
        android:paddingBottom="10dp"
        android:textSize="26sp"
        android:text="no data" />
</LinearLayout>

child.xml(二級列表):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/childTo"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:paddingTop="20dp"
        android:paddingLeft="50dp"
        android:paddingBottom="10dp"
        android:background="#00ff00"
        android:text="no data" />
</LinearLayout>

主程式 MainActivity

Activity 繼承 ExpandableListActivity,用巢狀的 Map 結構組織資料,再交給 SimpleExpandableListAdapter 綁定即可:

package com.example.expandlistaacitvity;

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.SimpleExpandableListAdapter;

public class MainActivity extends ExpandableListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 定義兩個一級條目
        List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
        Map<String, String> group1 = new HashMap<String, String>();
        group1.put("group", "group1 第一級列表 one");
        groups.add(group1);
        Map<String, String> group2 = new HashMap<String, String>();
        group2.put("group", "group2 第一級列表 two");
        groups.add(group2);

        // 第一個一級條目的二級條目
        List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
        Map<String, String> c1d1 = new HashMap<String, String>();
        c1d1.put("child", "child1 data1 第二級列表");
        child1.add(c1d1);
        Map<String, String> c1d2 = new HashMap<String, String>();
        c1d2.put("child", "child1 data2 第二級列表");
        child1.add(c1d2);

        // 第二個一級條目的二級條目
        List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
        Map<String, String> c2d1 = new HashMap<String, String>();
        c2d1.put("child", "child2 data1 第二級列表");
        child2.add(c2d1);

        // 將各二級列表彙整
        List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
        childs.add(child1);
        childs.add(child2);

        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
                this, groups, R.layout.group,
                new String[]{"group"}, new int[]{R.id.groupTo},
                childs, R.layout.child,
                new String[]{"child"}, new int[]{R.id.childTo});
        setListAdapter(adapter);
    }
}

Adapter 的參數依序是:上下文、一級資料、一級佈局、一級 key、一級控件 id、二級資料、二級佈局、二級 key、二級控件 id。搞懂這個順序,二級展開列表就難不倒你了。

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