This article from http://blog.csdn.net/hellogv/
GridView and ListView are more commonly used multi-control layout, and GridView is the first choice to implement nine chart! This article is about how to use the GridView to implement the nine diagram. The use of a lot of the GridView, the most online introduction of the method is to implement a imageadapter inherited Baseadapter, and then for the GridView use, similar to this method of this article no longer repeat, This article describes the GridView usage in front of the ListView in the very similar .... Also is I lazy, hee hee hehe ....
First to post the results of this article's Code run:
This article requires the addition/modification of 3 files: Main.xml, Night_item.xml, Java source code.
Main.xml source code as follows, itself is a girdview, for loading item:
[XHTML]View Plaincopyprint?
- <? 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"
- />
Describe some of the properties inside:
Android:numcolumns= "Auto_fit", the GridView column number is set to Automatic
Android:columnwidth= "90DP", width of each column, which is the width of item
Android:stretchmode= "ColumnWidth", Zoom and column size synchronization
Android:verticalspacing= "10DP", the margin between two lines, such as: line One (no.0~no.2) and row two (no.3~no.5) spacing is 10DP
Android:horizontalspacing= "10DP", the margin between two columns.
The next step is to introduce Night_item.xml, which is similar to the Imageitem.xml in the previous listview:
[XHTML]View Plaincopyprint?
- <? XML version= "1.0" encoding="Utf-8"?>
- <relativelayout
- xmlns:android="http://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>
The last is the source code of Java, but also with the previous ListView Java source is very similar, but more "selected" Event Processing:
[Java]View Plaincopyprint?
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- GridView GridView = (GridView) Findviewbyid (R.id.gridview);
- //Generate dynamic array and transfer to data
- arraylistnew arraylist
- For (int i=0;i<10;i++)
- {
- hashmap<string, object> map = new hashmap<string, object> ();
- Map.put ("Itemimage", R.drawable.icon); Add an ID for an image resource
- Map.put ("Itemtext", "NO." +string.valueof (i)); //Do Itemtext by serial number
- Lstimageitem.add (map);
- }
- //Generate adapter for Imageitem <====> dynamic array elements, both one by one corresponds
- Simpleadapter saimageitems = new Simpleadapter (this, //no explanation
- Lstimageitem,//Data source
- R.layout.night_item,XML implementation of//night_item
- //dynamic array with imageitem corresponding to the child
- New string[] {"itemimage","Itemtext"},
- //imageitem XML file inside of a imageview, two TextView ID
- new int[] {r.id.itemimage,r.id.itemtext});
- //Add and show
- 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 is clicked
- int arg2,//the position of the view in the adapter
- long Arg3//the row ID of the item that is clicked
- ) {
- //In this example ARG2=ARG3
- Hashmap<string, object> item= (hashmap<string, object>) arg0.getitematposition (ARG2);
- //Display the Itemtext of the selected item
- Settitle (String) item.get ("Itemtext");
- }
- }