編輯:Android 開發實例
GridView 與 ListView 都是常用的多項式版面元件,而要做九宮格,GridView 更是首選。網路上介紹 GridView 的文章多半是自己實作一個繼承 BaseAdapter 的 ImageAdapter,本文不重複那條路,改用與前面 ListView 極為類似的手法——用 SimpleAdapter 搭配動態陣列,寫法幾乎可以照搬,也省下不少工夫。
本例需要新增或修改三個檔案:main.xml、night_item.xml,以及 Java 原始碼。
main.xml 本身就是一個 GridView,作為 Item 的容器:
- <?xml version="1.0" encoding="utf-8"?>
- <GridView xmlns:android="https://schemas.android.com/apk/res/android"
- android:id="@+id/gridview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:numColumns="auto_fit"
- android:verticalSpacing="10dp"
- android:horizontalSpacing="10dp"
- android:columnWidth="90dp"
- android:stretchMode="columnWidth"
- android:gravity="center"
- />
幾個值得留意的屬性:
android:numColumns="auto_fit":欄數交由系統自動決定。
android:columnWidth="90dp":每欄寬度,也就是單個 Item 的寬度。
android:stretchMode="columnWidth":縮放基準與欄寬同步。
android:verticalSpacing="10dp":列與列之間的間距,例如第一列(NO.0~NO.2)與第二列(NO.3~NO.5)相隔 10dp。
android:horizontalSpacing="10dp":欄與欄之間的間距。
night_item.xml 定義單格的外觀,寫法與先前 ListView 的 item 幾乎相同:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip" android:layout_width="fill_parent">
- <ImageView
- android:layout_height="wrap_content"
- android:id="@+id/ItemImage"
- android:layout_width="wrap_content"
- android:layout_centerHorizontal="true">
- </ImageView>
- <TextView
- android:layout_width="wrap_content"
- android:layout_below="@+id/ItemImage"
- android:layout_height="wrap_content"
- android:text="TextView01"
- android:layout_centerHorizontal="true"
- android:id="@+id/ItemText">
- </TextView>
- </RelativeLayout>
Java 部分比 ListView 多了一段「選中」的事件處理。資料準備一樣用 ArrayList 搭配 HashMap,迴圈塞入十筆,圖片與文字分別對應 ItemImage 與 ItemText 兩個 key;接著交給 SimpleAdapter,指定 night_item 作為版面,並用兩個 int 陣列把 key 與元件 id 對應起來。
最後掛上 OnItemClickListener。回呼裡的四個參數分別代表事件所在的 AdapterView、被點擊的那個 View、該 View 在 adapter 中的位置,以及 row id。透過 getItemAtPosition(arg2) 取回整筆 HashMap,再取出 ItemText 寫進標題,就能確認點選的是哪一格。
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- GridView gridview = (GridView) findViewById(R.id.gridview);
- //生成動態數組,並且轉入數據
- ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
- for(int i=0;i<10;i++)
- {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("ItemImage", R.drawable.icon);//添加圖像資源的ID
- map.put("ItemText", "NO."+String.valueOf(i));//按序號做ItemText
- lstImageItem.add(map);
- }
- //生成適配器的ImageItem <====> 動態數組的元素,兩者一一對應
- SimpleAdapter saImageItems = new SimpleAdapter(this, //沒什麼解釋
- lstImageItem, //數據來源
- R.layout.night_item, //night_item的XML實現
- //動態數組與ImageItem對應的子項
- new String[] {"ItemImage","ItemText"},
- //ImageItem的XML文件裡面的一個ImageView,兩個TextView ID
- new int[] {R.id.ItemImage,R.id.ItemText});
- //添加並且顯示
- gridview.setAdapter(saImageItems);
- //添加消息處理
- gridview.setOnItemClickListener(new ItemClickListener());
- }
- //當AdapterView被單擊(觸摸屏或者鍵盤),則返回的Item單擊事件
- class ItemClickListener implements OnItemClickListener
- {
- public void onItemClick(AdapterView<?> arg0, //The AdapterView where the click happened
- View arg1, //The view within the AdapterView that was clicked
- int arg2, //The position of the view in the adapter
- long arg3 //The row id of the item that was clicked
- ) {
- //在本例中arg2=arg3
- HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);
- //顯示所選Item的ItemText
- setTitle((String)item.get("ItemText"));
- }
- }
Android 進階:SurfaceView 與多線程混搭實例
前文簡單介紹了 Android 中 SurfaceView 的基本用法,本文則進一步探討 SurfaceView 與多線程的混搭。之所以要將兩者結合,是為了避免動畫閃爍—
動手打造自訂 SurfaceView 元件:Android 圖像繪製系列(四)
當標準的 View 無法滿足需求時,自訂元件就成了開發者的好幫手。這一篇就來談談如何封裝一個可重複使用的 SurfaceView,無論是想呈現某個區域的動態畫面,還是打造
Java 開發環境建置(一):JDK 安裝與環境變數設定
要寫 Android,第一步是把 Java 的開發環境準備好。這一篇記錄 JDK 的安裝與 Windows 上兩個關鍵環境變數的設定流程,並附上驗證方式。 步驟一:下載
Android Activity 生命週期完整解析
Activity 是 Android 四大組件之一,其生命週期由系統依使用情境與資源狀態調用。理解各階段回呼,有助於正確管理資源與資料。四大基本狀態Activity 可分