Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 介面元件:GridView 網格檢視與自訂單元格

編輯:Android 開發實例

GridView 以列與行交錯的方式排列內容,最適合呈現圖示、縮圖這類尺寸接近的方塊資料。它的用法與 Gallery 類似,核心同樣是「提供一個 Adapter,其餘交給元件」。

兩個關鍵設定

第一是欄數。透過 setNumColumns() 決定每一列放幾個單元格,這個值會直接影響整體版面的密度。第二是單元格外觀,這需要在 Adapter 的 getView() 裡自行決定,GridView 不會幫你排版內部內容。

Activity 程式碼

package com.amaker.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private GridView gv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gv = (GridView)findViewById(R.id.GridView01);
        gv.setNumColumns(4);
        gv.setAdapter(new MyAdapter(this));
    }

    class MyAdapter extends BaseAdapter {

        private Integer[] imgs = {
            R.drawable.gallery_photo_1, R.drawable.gallery_photo_2,
            R.drawable.gallery_photo_3, R.drawable.gallery_photo_4,
            R.drawable.gallery_photo_5, R.drawable.gallery_photo_6,
            R.drawable.gallery_photo_7, R.drawable.gallery_photo_8,

            R.drawable.gallery_photo_1, R.drawable.gallery_photo_2,
            R.drawable.gallery_photo_3, R.drawable.gallery_photo_4,
            R.drawable.gallery_photo_5, R.drawable.gallery_photo_6,
            R.drawable.gallery_photo_7, R.drawable.gallery_photo_8
        };

        Context context;
        MyAdapter(Context context) {
            this.context = context;
        }

        public int getCount() {
            return imgs.length;
        }

        public Object getItem(int item) {
            return item;
        }

        public long getItemId(int id) {
            return id;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(45, 45));
                imageView.setAdjustViewBounds(false);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(imgs[position]);
            return imageView;
        }
    }
}

這個範例值得注意的細節

getView() 裡出現了 if (convertView == null) 的判斷,這是列表類元件的標準複用寫法:屬性設定(尺寸、縮放模式、內距)只在建立新 View 時做一次,後續則直接重用傳入的 convertView,只更新圖片內容。這樣可以大幅減少 View 的建立次數,是效能上最重要的一課。

縮放模式選用 CENTER_CROP 而非 FIT_XY,是因為網格單元格通常尺寸固定,用裁切填滿比變形拉伸美觀。若想保留完整圖形,可改為 FIT_CENTER 並搭配背景色填補。

另外,若資料只是簡單的字串陣列,也可以直接改用 ArrayAdapter,不必自己繼承 BaseAdapter;只有在需要自訂單元格外觀時,才有必要走自訂 Adapter 這條路。

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