Are you talking about this?
If so, let's look at the source code:
Gridview. xml Xml Code
- <? XmlVersion = "1.0" encoding = "UTF-8"?>
- <GridViewXmlns: 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 Code
- <? XmlVersion = "1.0" encoding = "UTF-8"?>
- <RelativeLayoutXmlns: 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 code
- 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;
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. gridview );
- Gridview = (GridView) findViewById (R. id. gridview );
- // Generate dynamic array and transfer data
- 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); // Add the ID of the image resource.
- Map. put ("ItemText", "NO." + String. valueOf (I); // do ItemText by serial number.
- LstImageItem. add (map );
- }
- // Generate the ImageItem of the adapter <====> dynamic array elements, one-to-one correspondence between the two
- SimpleAdapter saImageItems = new SimpleAdapter (this, // No explanation
- LstImageItem, // Data Source
- R. layout. item, // XML Implementation of night_item
- // Subitem of the dynamic array and ImageItem
- New String [] {"ItemImage", "ItemText "},
- // An ImageView in the XML file of ImageItem, two TextView IDS
- New int [] {R. id. ItemImage, R. id. ItemText });
- // Add and display
- Gridview. setAdapter (saImageItems );
- // Add Message Processing
- Gridview. setOnItemClickListener (new ItemClickListener ());
- }
- // When AdapterView is clicked (touch screen or keyboard), the returned Item Click Event
- 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
- ){
- // In this example, arg2 = arg3
- @ SuppressWarnings ("unchecked ")
- HashMap <String, Object> item = (HashMap <String, Object>) arg0.getItemAtPosition (arg2 );
- // Display the ItemText of the selected Item
- 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 s AvedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. gridview); gridview = (GridView) findViewById (R. id. gridview); // generates a dynamic array and transfers data to 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); // Add the IDmap of image resources. put ("ItemT Ext "," NO. "+ String. valueOf (I); // ItemTextlstImageItem by serial number. add (map) ;}// generate the ImageItem of the adapter <===> dynamic array elements. The two correspond to SimpleAdapter saImageItems = new SimpleAdapter (this, // There is no explanation for lstImageItem, // data source R. layout. XML implementation of item, // night_item // the subitem of the dynamic array and ImageItem new String [] {"ItemImage", "ItemText "}, // an ImageView in the XML file of ImageItem, two TextView IDnew int [] {R. id. itemImage, R. id. itemText}); // Add and display the gridview. set Adapter (saImageItems); // Add a message to process the gridview. setOnItemClickListener (new ItemClickListener ();} // when AdapterView is clicked (touch screen or keyboard), the returned Item clicks the event 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) {// in this example, arg2 = arg3 @ SuppressWarnings ("unchecked ") hashMap <String, Object> item = (HashMap <String, Object>) arg0.getItemAtPosition (arg2); // display ItemTextsetTitle (String) Item of the selected item. get ("ItemText "));}}}