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

Android ListView 與 ExpandableListView 實現多選、單選、全選與批量輸入

編輯:關於 Android 程式設計

下圖為列表多選與批量輸入的示意。

ListView 多選與全選

在 Adapter 中可用 Map 或 SparseArray 記錄每一列的勾選狀態。getView 時再依狀態設定 CheckBox。全選時,只要遍歷資料並把狀態設為 true,最後通知資料集更新即可。

RadioButton 單選

若每一列需要單選,例如到崗、遲到、休假、曠工,可在 ViewHolder 中放入多個 RadioButton,並用 OnCheckedChangeListener 更新對應資料。由於列表會重用 View,必須在 getView 中重新設定狀態,避免顯示錯誤。

ExpandableListView 子項選擇

使用 ExpandableListView 時,可在 onChildClick 中記錄被點選的群組與子項位置,再呼叫 notifyDataSetChanged 更新背景或圖示。

Edittext 批量輸入

若要在列表列輸入數量並即時加總,可為每個 EditText 加入 TextWatcher。因為 View 會重用,加入監聽前應先移除舊監聽,避免重複觸發。

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