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

以下範例示範如何用 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 繼承自 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 回收機制,在長列表上明顯更省記憶體;若只是維護既有專案,上面的邏輯仍然完全適用。
自定義View實作圓形旋轉唱片:手把手打造QQ音樂風格黑膠碟動畫
音樂播放器裡那張緩緩轉動的黑色唱片,幾乎已經成為「正在播放」的視覺符號。想在自己的 App 裡做出同樣的效果,其實不需要動畫框架,只要一個自定義 View 加上 Matr
360 手機衛士開啟來電顯示歸屬地:圖解設定流程
想知道來電是哪個地區打來的,用 360 手機衛士開啟歸屬地顯示就能做到。設定路徑並不深,照下面三步走完即可。 1. 安裝並打開 360 手機衛士,切換到「隱私保護」,點擊
用 CSS 控制 Android 控制項樣式:pixate-freestyle-android 入門
介紹 今天介紹一個開源專案:pixate-freestyle-android。它的概念很有意思——讓你用寫網頁時熟悉的 CSS 語法,來控制 Android 控制項的外觀
Firebase 上手 for Android:分析、用戶認證與實時數據庫
移動應用要做大一點,幾乎繞不開後端:用戶身份驗證、多設備數據同步、行為統計。可自建服務器意味著要懂運維、懂數據庫、懂接口設計——而多數獨立開發者的技能樹恰好缺這一支。 好