編輯:Android 開發實例
ExpandableListView 提供的是兩層結構的清單:一級群組標題可以展開或收合,底下再列出二級子項。像設定頁的分類選單、音樂 App 的「專輯 → 曲目」這類介面,都很適合用它。
實作時需要準備三份 XML,都放在 res/layout 底下。
<?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>
<?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>
<?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,縮排較淺),透過縮排深度讓層級關係一眼可辨。
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 即可。實際要放進哪些邏輯,取決於你的需求。
Android 手機衛士(十一):自訂焦點 TextView 實作跑馬燈
跑馬燈效果原理在 Android 中,TextView 要出現跑馬燈效果,通常需要單行顯示、設定 ellipsize 為 marquee,並讓元件取得焦點。若焦點不定時被
Android JSON 解析器:用 JSONObject 讀取天氣資料
為什麼 JSON 在 Android 中很重要 JSON 是一種輕量、易讀的資料交換格式,常用於 API 回傳。相較 XML,它結構更簡潔,解析也相對直接。Android
一看就懂的 Android App 開發入門:從環境到打包的完整示範
做過手機 App 項目的人都體會過那種分裂感:前端與 iOS 那邊配合起來基本順暢,而 Android 因為機型與 ROM 五花八門,問題層出不窮。正是這種麻煩勾起了我親
Android ProgressBar 教學:用 Spinner 提示背景工作
當 App 執行網路請求、讀取檔案或等待伺服器回應時,使用者需要知道目前有工作正在進行。Android 的 ProgressBar 可以分成兩種常見型態:進度條與 Spi