編輯:Android 開發實例
本篇整理 android.widget.SimpleExpandableListAdapter,對應 Android 2.3 r1 版本。
public abstract class SimpleExpandableListAdapter extends BaseExpandableListAdapter
java.lang.Object
android.widget.BaseExpandableListAdapter
android.widget.SimpleExpandableListAdapter
這是一個以 Map 儲存群組與子元素資料、並用 XML 資源檔定義外觀的靜態資料 adapter,可以想成 SimpleAdapter 的可展開版本。
它的資料結構是「List 的 List」:groupData 的每個項目對應 ExpandableListView 中的一個群組,而每個群組內部又是一個 Map 列表,分別對應該群組下的每一列子元素。群組與子元素各自指定一組 XML 配置檔與欄位對應關係——groupFrom 陣列裡的鍵值,會依序填入 groupTo 陣列所指定的 View id 中。
若可展開的層數超過一層,回傳的資料會被指定為 List 巢狀結構:第一層 List 對應子元素中代表的群組,第二層 List 對應孫子元素在該子群組中的位置,Map 亦支援這樣的巢狀元素。Android 自帶的 ApiDemos 中 App/View/ExpandableList3 就有示範。
SimpleExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData,
int groupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>>> childData,
int childLayout,
String[] childFrom, int[] childTo)
context:與此 adapter 關聯的 ExpandableListView 的上下文。groupData:Map 列表,每項對應一個群組,須包含 groupFrom 中指定的所有鍵。groupLayout:群組外觀的資源檔,必須包含 groupTo 裡所有的 View id。groupFrom / groupTo:欄位鍵值清單與對應的 View id 清單,前 N 個 View 會依序取得前 N 個欄位的資料。childData:Map 列表的列表。外層每項對應一個群組,內層每項對應該群組下的一列子元素。childLayout / childFrom / childTo:子元素的配置檔與欄位對應,規則同上。SimpleExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData,
int expandedGroupLayout, int collapsedGroupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>>> childData,
int childLayout,
String[] childFrom, int[] childTo)
與基本版本的差別在於把 groupLayout 拆成 expandedGroupLayout(群組展開時)與 collapsedGroupLayout(群組收合時)兩個參數,兩者都必須包含 groupTo 中所有指定的 View。
SimpleExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData,
int expandedGroupLayout, int collapsedGroupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>>> childData,
int childLayout, int lastChildLayout,
String[] childFrom, int[] childTo)
比上一版多了 lastChildLayout,用來指定「每個群組中最後一列子元素」的外觀。這個設計很實用——例如要在最後一列畫上不帶分隔線的樣式,或加上「載入更多」的提示,就靠它。
Object getChild(int groupPosition, int childPosition):取得指定子元素。long getChildId(int groupPosition, int childPosition):取得該子元素的 id。View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent):取得子元素的檢視。int getChildrenCount(int groupPosition):指定群組下的子元素數量。Object getGroup(int groupPosition):取得指定群組。int getGroupCount():群組總數。long getGroupId(int groupPosition):群組 id。View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent):取得群組檢視。boolean hasStableIds():是否具備穩定 id。boolean isChildSelectable(int groupPosition, int childPosition):子元素是否可取得焦點。View newChildView(boolean isLastChild, ViewGroup parent):建立新的子元素檢視。參數 isLastChild 表示是否為群組中的最後一項,parent 為新檢視的最終父層。View newGroupView(boolean isExpanded, ViewGroup parent):建立新的群組檢視。參數 isExpanded 表示該群組目前是否處於展開狀態。先組出 20 個群組、每組 15 筆子資料的巢狀結構,再交給 adapter:
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 20; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 15; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(children);
}
// 建立 adapter
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
範例直接用了系統內建的 simple_expandable_list_item_1 與 _2,兩者差別在於後者提供第二行文字,正好對應我們的兩個欄位。若換成自訂配置,務必確認 groupTo 與 childTo 中的每個 id 都真的存在於該配置檔裡,否則執行期會出錯。
另外,這個 adapter 是靜態的——資料在建立時就固定了。若需要動態增刪,記得操作原始 List 之後呼叫 notifyDataSetChanged();改動幅度較大時,直接重建 adapter 反而更省心。而 groupFrom/childFrom 的順序與 groupTo/childTo 的順序必須一一對應,這個「按位置配對」的規則是許多初學者出錯的地方,值得特別留意。
Android 使用 XML 打造動畫 UI 的深入解析
用 XML 定義動畫是 Android 中相當直覺的做法,能把淡入、淡出、縮放、旋轉等效果寫成資源檔,再在程式裡載入套用。本文從建立動畫資料夾開始,逐步說明載入、監聽與觸
Android 應用調試必備命令與實戰分析
寫代碼時我們習慣用斷點單步跟蹤,但上線之後遇到的問題往往沒那麼客氣:CPU 佔用莫名飆高、內存緩慢洩露、某個頁面偶發卡死……這些問題無法靠調試器復現,只能依靠系統日誌與性
用 NDK 加速彩圖轉灰階:Java 與原生碼的效能對比
把彩色圖片轉成灰階,用 Java 寫並不難,做法跟 J2ME 時代幾乎一樣:讀出每個像素的 RGB,取平均,再組回一個像素。問題出在規模——圖片一大,或者需要連續處理多張
Android ApiDemo 系列三:Activity 切換動畫
ApiDemo 的 Animation 範例示範在切換 Activity 時加入動畫效果。常見的兩種效果是淡入與縮放:淡入讓新畫面由透明逐漸顯示,縮放則讓新畫面由小變大出