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

用 LayerDrawable 實作 GridView 多選勾選效果

編輯:關於 Android 程式設計

使用 GridView 時常會遇到需要多選的情境,例如批次刪除圖片、批次上傳相簿。這類需求不必急著去找現成的 checkbox 元件,透過「層疊圖」把勾選標記疊在縮圖上,就能做出乾淨俐落的視覺回饋。

一、版面配置

外層用 LinearLayout 包住 GridView,設定三欄與間距:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:background="#000000"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <GridView
        android:id="@+id/grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="3"
        android:verticalSpacing="3dp"
        android:horizontalSpacing="3dp" />
</LinearLayout>

每一格的 item.xml 只放一張 ImageView,尺寸固定 70dp:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image_item"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:padding="3dp"
        android:scaleType="fitXY" />
</LinearLayout>

二、Activity:把點擊交給 Adapter

Activity 這邊不需要自己維護狀態,只要把點擊位置轉給 adapter 即可:

GridView gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new Adpter(image, this));
gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long id) {
        adpter.chiceState(position);
    }
});

三、Adapter:核心就在這裡

用一個 boolean 陣列記錄每一格的勾選狀態,getView 時再依狀態決定要疊幾層圖:

public class Adpter extends BaseAdapter {
    private int[] image;
    private boolean[] isChice;
    private Context context;

    public Adpter(int[] im, Context context) {
        this.image = im;
        this.context = context;
        isChice = new boolean[im.length];   // 預設全為 false
    }

    @Override public int getCount() { return image.length; }
    @Override public Object getItem(int p) { return image[p]; }
    @Override public long getItemId(int p) { return p; }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        GetView holder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item, null);
            holder = new GetView();
            holder.imageView = (ImageView) view.findViewById(R.id.image_item);
            view.setTag(holder);
        } else {
            holder = (GetView) view.getTag();
        }
        holder.imageView.setImageDrawable(getView(position));
        return view;
    }

    // 把原圖與勾選標記疊成一個 LayerDrawable
    private LayerDrawable getView(int post) {
        Bitmap bitmap = ((BitmapDrawable) context.getResources()
                .getDrawable(image[post])).getBitmap();
        Bitmap mark = null;
        if (isChice[post]) {
            mark = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.editable_mode_checked_tag);
        }
        LayerDrawable la;
        if (mark != null) {
            la = new LayerDrawable(new Drawable[]{
                    new BitmapDrawable(bitmap), new BitmapDrawable(mark)});
            la.setLayerInset(0, 0, 0, 0, 0);
            la.setLayerInset(1, 0, 65, 65, 0);  // 讓標記往右下角靠
        } else {
            la = new LayerDrawable(new Drawable[]{new BitmapDrawable(bitmap)});
            la.setLayerInset(0, 0, 0, 0, 0);
        }
        return la;
    }

    public void chiceState(int post) {
        isChice[post] = !isChice[post];
        notifyDataSetChanged();
    }

    static class GetView { ImageView imageView; }
}

幾個實作重點

之所以選 LayerDrawable 而不是在 item 佈局裡多塞一個 ImageView,是因為勾選框需要精準壓在縮圖的右下角,用 setLayerInset 控制第二層的內縮距離,比再調一次 margin、gravity 來得直接。另外別忘了 getView 裡的 convertView 複用,否則長列表滾動時會不斷重建 View 造成卡頓。若要進一步擴充成真正的批次操作,只要多提供一個方法回傳所有被勾選的 position 集合,交給外層做批次刪除或上傳即可。

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