Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Android 中文 API:BaseExpandableListAdapter 詳解

編輯:進階開發

本篇整理 android.widget.BaseExpandableListAdapter 的中文說明,版本為 Android 2.3 r1,適合想深入理解可展開列表的開發者參考。

類別結構

public abstract class BaseExpandableListAdapter extends Object
implements ExpandableListAdapter, HeterogeneousExpandableList

直接子類為 CursorTreeAdapter、SimpleExpandableListAdapter;間接子類則有 ResourceCursorTreeAdapter、SimpleCursorTreeAdapter。

概述

BaseExpandableListAdapter 是 ExpandableListAdapter 的抽象基類,能從資料來源提供內容與檢視給可展開列表。所有繼承本類的 Adapter,都必須正確實作 getCombinedChildId(long, long) 與 getCombinedGroupId(long),以便從檢視群組或子元素的 ID 中產生唯一識別碼。

這裡的「群組」指的是可展開的列表項,點開後會看到多個「子元素」。由於單純用一般 ID 尋找群組與子元素不夠方便,因此採用聯合 ID 的方式來解決。Android 自帶的 ApiDemos(App/View/ExpandableList1)就有相關範例。

主要方法

・getChildType / getChildTypeCount:取得子元素的型別與型別數量。

・getGroupType / getGroupTypeCount:取得群組的型別與型別數量,預設皆回傳 1。

・getCombinedChildId / getCombinedGroupId:以位元組的方式,把群組 ID 與子元素 ID 組合成全域唯一的 long 值。若 hasStableIds() 為 true,還必須回傳穩定的 ID。

・areAllItemsEnabled / isEmpty:判斷是否啟用全部項目、以及資料是否為空。

・notifyDataSetChanged / notifyDataSetInvalidated:當背景資料集變更或失效時呼叫,用來通知列表刷新。

・onGroupCollapsed / onGroupExpanded:分別在群組收合、展開時觸發。

・registerDataSetObserver / unregisterDataSetObserver:註冊與取消註冊資料觀察器。

掌握這些方法,就能靈活實作多層級、可展開收合的清單介面。

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