你說的是這樣子嗎?
如果是這樣子的話就看源碼吧:
gridview.xml Xml代碼
- <?xml version="1.0" encoding="utf-8"?>
- <GridView xmlns:android="http://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"
- />
<?xml version="1.0" encoding="utf-8"?><GridView xmlns:android="http://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" />
item.xml Xml代碼
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:paddingBottom="4dip"
- >
- <ImageView
- android:id="@+id/ItemImage"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:layout_centerHorizontal="true"
- />
- <TextView
- android:id="@+id/ItemText"
- android:layout_width="wrap_content"
- android:layout_below="@+id/ItemImage"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:text="TextView01"
- />
- </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="wrap_content"android:layout_width="fill_parent"android:paddingBottom="4dip"><ImageViewandroid:id="@+id/ItemImage"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerHorizontal="true"/><TextViewandroid:id="@+id/ItemText"android:layout_width="wrap_content"android:layout_below="@+id/ItemImage"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="TextView01"/></RelativeLayout>
activity Java代碼
- package com.test;
-
- import java.util.ArrayList;
- import java.util.HashMap;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.GridView;
- import android.widget.SimpleAdapter;
-
- public class TestGridView extends Activity {
- private GridView gridview;
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.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.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
- @SuppressWarnings("unchecked")
- HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
- // 顯示所選Item的ItemText
- setTitle((String) item.get("ItemText"));
- }
- }
- }
package com.test;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.GridView;import android.widget.SimpleAdapter;public class TestGridView extends Activity {private GridView gridview;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.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);// 添加映像資源的IDmap.put("ItemText", "NO." + String.valueOf(i));// 按序號做ItemTextlstImageItem.add(map);}// 產生適配器的ImageItem <====> 動態數組的元素,兩者一一對應SimpleAdapter saImageItems = new SimpleAdapter(this, // 沒什麼解釋lstImageItem,// 資料來源R.layout.item,// night_item的XML實現// 動態數組與ImageItem對應的子項new String[] { "ItemImage", "ItemText" },// ImageItem的XML檔案裡面的一個ImageView,兩個TextView IDnew 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 happenedView arg1,// The view within the AdapterView that was clickedint arg2,// The position of the view in the adapterlong arg3// The row id of the item that was clicked) {// 在本例中arg2=arg3@SuppressWarnings("unchecked")HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);// 顯示所選Item的ItemTextsetTitle((String) item.get("ItemText"));}}}