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

Android 開發實作:ListView 條目的批量選擇與刪除

編輯:關於 Android 程式設計

對於一個可編輯、可刪除的 ListView 列表來說,提供批量刪除功能幾乎是標配。否則使用者只能一項一項地刪除,不僅體驗糟糕,也平添許多麻煩。本文將一步步實現這樣的效果。

實現效果圖如下:

主介面布局 select.xml

主介面由一個 ListView 與一個隱藏的操作列組成,操作列包含「撤銷」、「已選數量」和「刪除」三個部分:

<?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" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:cacheColorHint="#FFF" >
    </ListView>

    <RelativeLayout
        android:id="@+id/relative"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:gravity="bottom"
        android:background="@color/lemonchiffon"
        android:visibility="gone" >
        <Button
            android:id="@+id/cancle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="撤銷 |"
            android:textSize="20sp"
            android:background="@null"
            android:layout_centerVertical="true" />
        <TextView
            android:id="@+id/txtcount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="共計"
            android:textSize="15sp"
            android:layout_centerInParent="true" />
        <Button
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="| 刪除"
            android:textSize="20sp"
            android:background="@null"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>
</LinearLayout>

條目布局 item.xml

每個列表項包含一個 TextView 顯示文字,一個 CheckBox 供勾選:

<?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="horizontal" >
    <TextView
        android:id="@+id/txtName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:layout_weight="1"
        android:text="444444444444"
        android:textSize="17sp"
        android:textColor="#333" />
    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:clickable="false" />
</LinearLayout>

自定義 Adapter 與長按監聽

透過自定義 Adapter 綁定 ListView 的數據源,並實作長按監聽。長按進入多選模式時,切記要讓監聽事件回傳 true,以攔截普通的點擊行為。

/**
 * 自定義 Adapter
 */
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>();
        ...
    }
    ...
}

完整實作

下面是包含 Activity 邏輯與 Adapter 的完整程式碼:

package com.example.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MulSelect extends Activity implements OnClickListener {

    private ListView listview;
    private Context context;
    private List<String> array = new ArrayList<String>();
    private List<String> selectid = new ArrayList<String>();
    private boolean isMulChoice = false; // 是否多選
    private Adapter adapter;
    private RelativeLayout layout;
    private Button cancle, delete;
    private TextView txtcount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select);
        context = this;
        listview = (ListView) findViewById(R.id.list);
        layout = (RelativeLayout) findViewById(R.id.relative);
        txtcount = (TextView) findViewById(R.id.txtcount);
        cancle = (Button) findViewById(R.id.cancle);
        delete = (Button) findViewById(R.id.delete);
        cancle.setOnClickListener(this);
        delete.setOnClickListener(this);
        init();
        adapter = new Adapter(context, txtcount);
        listview.setAdapter(adapter);
    }

    void init() {
        for (int i = 0; i < 20; i++) {
            array.add("小明" + i);
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancle:
                isMulChoice = false;
                selectid.clear();
                adapter = new Adapter(context, txtcount);
                listview.setAdapter(adapter);
                layout.setVisibility(View.INVISIBLE);
                break;
            case R.id.delete:
                isMulChoice = false;
                for (int i = 0; i < selectid.size(); i++) {
                    // 依選中的 id 從資料源中移除對應條目
                }
                break;
        }
    }
    ...
}

核心思路是:透過兩個 HashMap 分別記錄每個條目的 CheckBox 是否顯示、是否被勾選;進入長按多選模式後顯示操作列,勾選時即時更新已選數量;點擊刪除時遍歷已選集合,從數據源移除並刷新 Adapter。這樣就完成了一個流暢的批量選擇與刪除功能。

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