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

Android 入門第八篇:GridView 實作九宮格

編輯:Android 開發實例

GridView 與 ListView 都是常用的多項式版面元件,而要做九宮格,GridView 更是首選。網路上介紹 GridView 的文章多半是自己實作一個繼承 BaseAdapter 的 ImageAdapter,本文不重複那條路,改用與前面 ListView 極為類似的手法——用 SimpleAdapter 搭配動態陣列,寫法幾乎可以照搬,也省下不少工夫。

三個檔案

本例需要新增或修改三個檔案:main.xml、night_item.xml,以及 Java 原始碼。

main.xml 本身就是一個 GridView,作為 Item 的容器:

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <GridView xmlns:android="https://schemas.android.com/apk/res/android"   
  3.     android:id="@+id/gridview" 
  4.     android:layout_width="fill_parent"   
  5.     android:layout_height="fill_parent"
  6.     android:numColumns="auto_fit" 
  7.     android:verticalSpacing="10dp" 
  8.     android:horizontalSpacing="10dp" 
  9.     android:columnWidth="90dp" 
  10.     android:stretchMode="columnWidth" 
  11.     android:gravity="center" 
  12. /> 

幾個值得留意的屬性:

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 幾乎相同:

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout   
  3.          xmlns:android="https://schemas.android.com/apk/res/android"   
  4.          android:layout_height="wrap_content"   
  5.          android:paddingBottom="4dip" android:layout_width="fill_parent"> 
  6.          <ImageView   
  7.                android:layout_height="wrap_content"   
  8.                android:id="@+id/ItemImage"   
  9.                android:layout_width="wrap_content"   
  10.                android:layout_centerHorizontal="true">   
  11.          </ImageView> 
  12.          <TextView   
  13.                android:layout_width="wrap_content"   
  14.                android:layout_below="@+id/ItemImage"   
  15.                android:layout_height="wrap_content"   
  16.                android:text="TextView01"   
  17.                android:layout_centerHorizontal="true"   
  18.                android:id="@+id/ItemText"> 
  19.          </TextView> 
  20. </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 寫進標題,就能確認點選的是哪一格。

 
  1. public void onCreate(Bundle savedInstanceState) {     
  2.       super.onCreate(savedInstanceState);     
  3.       setContentView(R.layout.main);     
  4.       GridView gridview = (GridView) findViewById(R.id.gridview);     
  5.            
  6.       //生成動態數組,並且轉入數據     
  7.       ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();     
  8.       for(int i=0;i<10;i++)     
  9.       {     
  10.         HashMap<String, Object> map = new HashMap<String, Object>();     
  11.         map.put("ItemImage", R.drawable.icon);//添加圖像資源的ID     
  12.     map.put("ItemText", "NO."+String.valueOf(i));//按序號做ItemText     
  13.         lstImageItem.add(map);     
  14.       }     
  15.       //生成適配器的ImageItem <====> 動態數組的元素,兩者一一對應     
  16.       SimpleAdapter saImageItems = new SimpleAdapter(this, //沒什麼解釋     
  17.                                                 lstImageItem,  //數據來源      
  18.                                                 R.layout.night_item,  //night_item的XML實現     
  19.                                                      
  20.                                                 //動態數組與ImageItem對應的子項             
  21.                                                 new String[] {"ItemImage","ItemText"},      
  22.                                                      
  23.                                                 //ImageItem的XML文件裡面的一個ImageView,兩個TextView ID     
  24.                                                 new int[] {R.id.ItemImage,R.id.ItemText});     
  25.       //添加並且顯示     
  26.       gridview.setAdapter(saImageItems);     
  27.       //添加消息處理     
  28.       gridview.setOnItemClickListener(new ItemClickListener());     
  29.   }     
  30.        
  31.   //當AdapterView被單擊(觸摸屏或者鍵盤),則返回的Item單擊事件     
  32.   class  ItemClickListener implements OnItemClickListener     
  33.   {     
  34. public void onItemClick(AdapterView<?> arg0,  //The AdapterView where the click happened      
  35.                                   View arg1,  //The view within the AdapterView that was clicked     
  36.                                   int arg2,  //The position of the view in the adapter     
  37.                                   long arg3  //The row id of the item that was clicked     
  38.                                   ) {     
  39.     //在本例中arg2=arg3     
  40.     HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);     
  41.     //顯示所選Item的ItemText     
  42.     setTitle((String)item.get("ItemText"));     
  43. }     
  44.           
  45.   } 
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved