編輯:關於 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 直接繼承 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;
}
}
@android:id/list,因為 ExpandableListActivity 就是靠它來找控制項。
Android 學習筆記:Switch 開關控件的使用案例
Switch 是一個帶有開關效果的控件,常用來表示某個功能的開啟或關閉狀態。以下透過一個簡單範例說明它的布局與事件處理。 布局程式碼 控制程式碼
以自定義滑動開關按鈕為例,圖文透視 Android 自定義 View 繪制
自定義 View 常常是 Android 開發者面前的一道坎。本文不談空洞理論,直接拿一個滑動開關按鈕開刀,把 measure、layout、draw 三個環節串起來講清
Android 學習實作:打造簡易版微信(二)之登入與註冊
1 概述 從這篇開始,正式進入簡易版微信的開發。在深入之前,想先聊聊個人對 Android 程式開發的一些理解,未必正確,僅是自家心得。Android 開發不像大學時寫
Android 全域未捕捉異常與崩潰處理實戰
本文示範如何為 App 建立一道「安全網」:當程式爆發未捕捉異常時,不再直接閃退,而是彈出一個友善的提示對話框,同時把完整的錯誤堆疊自動寫入 SD 卡的 crash 目錄