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

ExpandableListView 深入解析:屬性、Adapter 與自定義箭頭

編輯:關於 Android 開發

ExpandableListView 深入解析:屬性、Adapter 與自定義箭頭

ExpandableListView 是可展開的分組列表控制項,常用於分類選單、設定頁與檔案目錄。本文整理常用 XML 屬性、BaseExpandableListAdapter 實作重點與自定義箭頭方式。

常用 XML 屬性

  • android:listSelector:設定點擊項目的背景色。
  • android:dividerHeight:設定分組分隔線高度。
  • android:childDivider:設定子項分隔線。
  • android:groupIndicator:可設為 @null 以自定義箭頭。
  • android:scrollingCache:可關閉滾動快取,避免背景異常。

Adapter 重點

繼承 BaseExpandableListAdapter 後,主要需實作 getGroupCount、getChildCount、getGroup、getChild、getGroupView、getChildView 等方法。

getGroupView 負責繪製分組標題,getChildView 負責繪製子項目。兩者都可使用 ViewHolder 減少 findViewById 次數。

自定義箭頭

將 groupIndicator 設為 @null,並在 getGroupView 中依 isExpanded 切換箭頭圖示,即可自定義展開與收合狀態。

資料結構

通常使用 List<Map<String, Object>> 表示分組,使用 List<List<Map<String, Object>>> 表示各分組下的子項目。

結果展示

整體而言,ExpandableListView 適合需要層級式資料呈現的畫面,搭配自定義 Adapter 後可高度客製化樣式與互動。

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