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

Android GridView 多選效果實作:LayerDrawable 疊加勾選標記

編輯:關於 Android 程式設計

相簿、多圖上傳這類場景,往往需要讓使用者在 GridView 中點選多張圖。核心技巧是用 LayerDrawable 把「原圖」與「勾選標記」兩張圖疊在一起,並用一個 boolean 陣列記錄每一格的就選狀態。

先看成果:

一、版面:main.xml

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

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

</LinearLayout>

二、單格版面: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" >

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

三、MainActivity:把點擊事件轉給 Adapter

public class MainActivity extends Activity {
    private Adpter adpter;
    private GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        int[] image = { R.drawable.dog1, R.drawable.m2, R.drawable.m3,
                        R.drawable.m4, R.drawable.m5, R.drawable.dog2 };
        adpter = new Adpter(image, this);
        gridView = (GridView) findViewById(R.id.grid);
        gridView.setAdapter(adpter);
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                adpter.chiceState(position);   // 反轉該格選取狀態
            }
        });
    }
}

四、Adpter:狀態陣列與圖層疊加

public class Adpter extends BaseAdapter {
    private int[] image;
    private boolean[] isChice;      // 記錄每一格的選取狀態
    private Context context;

    public Adpter(int[] im, Context context) {
        this.image = im;
        isChice = new boolean[im.length];
        for (int i = 0; i < im.length; i++) isChice[i] = false;
        this.context = context;
    }

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

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        View view = arg1;
        GetView holder = null;
        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(arg0));
        return view;
    }

    static class GetView {
        ImageView imageView;
    }

    // 主要邏輯:把原圖與勾選標記疊成 LayerDrawable
    private LayerDrawable getView(int post) {
        Bitmap bitmap = ((BitmapDrawable)
                context.getResources().getDrawable(image[post])).getBitmap();
        Bitmap bitmap2 = null;
        if (isChice[post]) {
            bitmap2 = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.editable_mode_checked_tag);
        }
        Drawable[] array;
        if (bitmap2 != null) {
            array = new Drawable[2];
            array[0] = new BitmapDrawable(bitmap);
            array[1] = new BitmapDrawable(bitmap2);
            LayerDrawable la = new LayerDrawable(array);
            la.setLayerInset(0, 0, 0, 0, 0);      // 底圖不縮放
            la.setLayerInset(1, 0, 65, 65, 0);    // 標記往左上角偏移
            return la;
        } else {
            array = new Drawable[1];
            array[0] = new BitmapDrawable(bitmap);
            LayerDrawable la = new LayerDrawable(array);
            la.setLayerInset(0, 0, 0, 0, 0);
            return la;
        }
    }

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

關鍵重點

  • 狀態與 View 分離:勾選狀態存在 Adapter 的 boolean 陣列裡,不依賴 View 本身的 tag,因為 ListView/GridView 會重用 view。
  • LayerDrawable.setLayerInset:參數順序是 layerIndex、left、top、right、bottom,用來控制每一層的內縮距離,讓勾選標記落在右上角或左上角。
  • notifyDataSetChanged:反轉狀態後必須主動通知刷新,畫面才會重繪。

若要擴充成「最多選九張」或「全選」,只要在 chiceState 中加上數量判斷即可,不必改動 getView 的邏輯。

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