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

Android GridView 網格檢視的使用教學

編輯:關於 Android 程式設計

GridView 以列與行的方式排列內容,最常見的用途是顯示圖片,例如九宮格入口。它的排列邏輯近似矩陣:當畫面上有大量元素需要以矩陣格式呈現時,GridView 就是首選,而使用它的核心其實只有一個——設定 Adapter。

常用 XML 屬性

  • android:columnWidth:每一列的寬度。
  • android:gravity:每個網格的對齊比重。
  • android:horizontalSpacing:網格之間的水平間距。
  • android:numColumns:列數。
  • android:stretchMode:列以何種方式填滿可用空間。
  • android:verticalSpacing:網格之間的垂直間距。

實作目標

以下範例示範如何用 GridView 排出四列的 App 圖示,並在每格顯示圖示與名稱。

圖1 主畫面顯示效果

一、主畫面版面

用 LinearLayout 做垂直排列,內含一個 GridView:

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

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="4"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp">
    </GridView>
</LinearLayout>

這裡用 numColumns="4" 指定四列,並以 horizontalSpacing 與 verticalSpacing 設定 10dp 的間距。

二、單格版面

每一格要顯示圖示與名稱,因此在 layout 下新增 griditeminfo.xml,用 RelativeLayout 把 ImageView 放上方、TextView 放下方,兩者皆水平居中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/itemImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
    </ImageView>

    <TextView
        android:id="@+id/itemName"
        android:layout_below="@+id/itemImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
    </TextView>
</RelativeLayout>

三、資料存放

圖示與名稱是一一對應的關係,因此用 HashMap 存一組配對,再把每個 HashMap 塞進 ArrayList:

// 將圖示與名稱存入 ArrayList
ArrayList<HashMap<String, Object>> item = new ArrayList<HashMap<String, Object>>();

for (int i = 0; i < resIds.length; i++) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("itemImage", resIds[i]);
    map.put("itemName", name[i]);
    item.add(map);
}

其中 resIds[] 存圖示資源 id,name[] 存名稱。

四、SimpleAdapter 綁定資料

SimpleAdapter 繼承自 BaseAdapter,用來把靜態資料對應到 XML 中定義好的檢視,資料來源通常就是由 Map 組成的 List。

public SimpleAdapter(Context context,
                     List<? extends Map<String, ?>> data,
                     int resource,
                     String[] from,
                     int[] to);

參數依序是:關聯的上下文、Map 清單、項目版面資源、Map 中的鍵名稱陣列、版面中要對應的 view id 陣列。

SimpleAdapter simpleAdapter = new SimpleAdapter(
        this, item, R.layout.griditeminfo,
        new String[] {"itemImage", "itemName"},
        new int[] {R.id.itemImage, R.id.itemName});

完成後掛到 GridView:

mGridView.setAdapter(simpleAdapter);

五、事件監聽

實務上要能知道使用者點了哪一格。GridView 常用兩個監聽器:OnItemSelectedListener 處理「選中」,OnItemClickListener 處理「點擊」。各自實作介面即可:

// OnItemClickListener
public void onItemClick(AdapterView<?> parent, View view, int position, long id);

// OnItemSelectedListener
public void onItemSelected(AdapterView<?> parent, View view, int position, long id);
public void onNothingSelected(AdapterView<?> parent);

其中 position 就是該格的索引,用它去 item.get(position) 取回資料即可。補充一點:GridView 已在近年被標示為不建議使用,官方更推薦 RecyclerView 搭配 GridLayoutManager,因為它內建 view 回收機制,在長列表上明顯更省記憶體;若只是維護既有專案,上面的邏輯仍然完全適用。

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