編輯:關於 Android 程式設計
上一節完成了界面設計,這一節來處理具體功能:怎麼把黑名單 / 白名單顯示出來,以及更關鍵的——怎麼刪除。
如果只支援「長按彈出選單刪一筆」或「一鍵清空全部」,都不符合真實需求。實務上使用者的典型行為是一次刪掉好幾筆。所以問題變成:如何在 ListView 中實現多選?答案就是 CheckBox。


進入刪除模式後:底部按鈕換成「取消 / 已選數量 / 刪除」三件套,每個 Item 右側出現 CheckBox。這一節先完成顯示與監聽器綁定,增刪的資料層邏輯留到下一節。
list_item.xml 的關鍵是用可見性切換表達兩種模式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/layout_item">
<TextView
android:id="@+id/tv_id"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_margin="3dp"
android:text="jesson"
android:layout_toLeftOf="@+id/cb_choose" />
<TextView
android:id="@+id/tv_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_margin="3dp"
android:text="18182446925"
android:layout_toLeftOf="@+id/cb_choose"
android:layout_below="@id/tv_name" />
<CheckBox
android:id="@+id/cb_choose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="invisible" />
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/line" />
</LinearLayout>
三個設計細節值得注意。
tv_id 設為 gone 而非刪除。把資料主鍵隱藏存放在 item 裡,是列表開發的常用手法——提交刪除時直接從對應 item 取出 id,不必再靠 position 反查資料源(position 在刪除後會錯位)。
CheckBox 用 invisible 而非 gone。兩者差別在於佔不佔空間。這裡刻意選 invisible,讓文字與 CheckBox 之間的間距在兩種模式下保持一致,切換時不會產生跳動。若用 gone,CheckBox 從布局中移除,tv_name 的 layout_toLeftOf 會失去約束,文字寬度會突然變長。
用 RelativeLayout 讓文字為 CheckBox 讓位。layout_toLeftOf="@+id/cb_choose" 讓兩行文字都避開右側的 CheckBox,這比寫死 margin 更能適應不同螢幕寬度。
public class Person {
String name;
String number;
int id;
public Person(int id, String name, String number) {
this.id = id;
this.name = name;
this.number = number;
}
}
把 id 放在第一個參數並用建構子強制賦值,是為了提醒呼叫方「id 一定要給」,避免新增時忘記導致後續刪列出錯。
class Adapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater = null;
private HashMap<Integer, View> mView;
public HashMap<Integer, Integer> visiblecheck; // 記錄是否顯示 CheckBox
public HashMap<Integer, Boolean> ischeck; // 記錄各項是否被勾選
private TextView txtcount;
public Adapter(Context context, TextView txtcount) {
this.context = context;
this.txtcount = txtcount;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = new HashMap<Integer, View>();
visiblecheck = new HashMap<Integer, Integer>();
ischeck = new HashMap<Integer, Boolean>();
if (isMulChoice) {
for (int i = 0; i < array.size(); i++) {
visiblecheck.put(i, View.VISIBLE);
ischeck.put(i, false);
}
}
}
// getCount / getItem / getItemId 依標準實作
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 依 visiblecheck 決定 CheckBox 顯示與否
// 依 ischeck 還原勾選狀態
// 依 isMulChoice 決定底部操作列的樣式
return convertView;
}
}
這裡用了兩個 Map 來承載兩種彼此獨立的狀態,這個拆分很值得學:
visiblecheck(是否顯示 CheckBox)——由「目前是否處於刪除模式」決定,對所有 item 一致。
ischeck(是否被勾選)——每個 item 各自獨立。
若把兩者混成一個變數,會無法表達「處於刪除模式但這筆沒勾」的情況。狀態分離是列表多選最容易出錯的地方,先想清楚狀態空間再寫 getView 能省下大量除錯時間。
mView 這個 HashMap<Integer, View> 的用意是保存各 item 的 View 引用,方便在外部直接操作某幾筆的 CheckBox 而不必等待 ListView 重新繪製。這是必要之惡——它同時也是風險點,因為持有 View 引用會妨礙複用,實作時要確保資料變更後呼叫 notifyDataSetChanged() 讓狀態統一刷新。
private List<Person> array = new ArrayList<Person>(); // 全部資料 private List<Person> selectid = new ArrayList<Person>(); // 已勾選的資料 private ListView lv_show; private CheckBox cb; private TextView tv_count; private RelativeLayout add_layout; private RelativeLayout delete_layout; private Adapter adapter; tv_count = (TextView) findViewById(R.id.tv_select); add_layout = (RelativeLayout) findViewById(R.id.add_layout); delete_layout = (RelativeLayout) findViewById(R.id.delete_layout); lv_show = (ListView) findViewById(R.id.lv_show);
這裡同樣是兩個清單的設計:array 保存完整資料,selectid 只保存被勾選的那幾筆。提交刪除時直接遍歷 selectid,不必再回頭檢查每個 CheckBox 的狀態——這是 O(n) 而非 O(n²) 的做法。
OnLongClick 是這裡的入口。長按 ListView 的任一 item,觸發三件事:
1. 把所有 item 的 CheckBox 設為可見(遍歷更新 visiblecheck)
2. 切換底部操作列:隱藏 add_layout、顯示 delete_layout
3. 呼叫 adapter.notifyDataSetChanged() 讓 ListView 重繪以套用新狀態
這裡再次用到了控制項的隱藏與顯示——這是整个刪除模式切換的視覺基礎。剩下的就是各控制項的宣告、findViewById、監聽器綁定,以及點擊 CheckBox 時同步更新 ischeck 與 selectid、刷新 tv_count 顯示已選數量。
ListView 的 item 內部有可點擊控制項時,OnItemClickListener 會失效。CheckBox 預設是 focusable 的,它會吃掉觸摸事件。解法是在 item 布局中為 CheckBox 加上:
android:focusable="false" android:clickable="false"
或者乾脆不用 CheckBox 的內建點擊,改成在 getView 裡給整列掛 OnClickListener 自行切換狀態。第二種做法體驗更好——使用者的觸控區域是整列而非小小的方框。
convertView 復用會導致狀態錯亂。ListView 會重複使用 View 物件,若不每次都依 ischeck 顯式設定 setChecked(),就會出現「捲動之後勾選狀態亂掉」的經典 bug。原則是:getView 中所有依賴資料的視覺屬性都必須每次重新設定,不能依賴 View 的當前狀態。
刪除後的索引問題。用 selectid 存物件而非 position 存索引,就能天然避開這個問題——物件引用不會因為前面刪了幾筆而錯位。
完整程式碼可參考原文提供的打包下載。核心觀念整理成一句:用兩個狀態 Map 分離「模式」與「勾選」,用兩個 List 分離「全部」與「已選」,剩下的都是視覺切換。
微信小程序頁面跳轉:navigator 與 wx.navigateTo 用法
頁面跳轉的兩種方式 微信小程序可以透過 WXML 中的 navigator 元件進行跳轉,也可以在 JavaScript 中呼叫導航 API。兩種方式各有適用情境,開發者
Android Camera 拍照實戰:從十步流程到填坑細節
本文整理我在專案中實作拍照功能時對 Camera 的使用心得。之所以選用 Camera 而非 camera2,是因為 camera2 直到 API 21(5.0.1)才引
Android 記憶體洩漏的簡單檢查與分析方法
導語 記憶體洩漏大概是 Android 開發者最頭疼的問題之一。本文整理一下檢查與分析記憶體洩漏常用的一些工具與方法。 一、什麼是記憶體洩漏? Java 有垃圾回收機制,
Android 繪圖 API 總整理:Bitmap、Canvas、Paint、Path 一次搞懂
只要做過自訂控制項,就一定會跟這幾個傢伙打照面:Bitmap 負責裝圖,Canvas 負責畫布,Paint 負責畫筆,Path 負責軌跡。Android 在這塊提供的支援