Both the gridview and listview are commonly used multi-control la S, and the gridview is the first choice to implement the jiugong diagram! This article describes how to use the gridview to implement the jiugong diagram. The most widely used method on the Internet is to implement an imageadapter to inherit the baseadapter and use it for the gridview. This method is not repeated in this article, the usage of the gridview described in this article is very similar to that of the previous listview .... I am also a little lazy ....
Post this article firstCodeRunning result:
In this article, you need to add/modify three files: Main. XML, night_item.xml, and Java.Source code.
The source code of Main. XML is as follows, which is a girdview, used to load items:
[XHTML] View plaincopyprint?
-
- <?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"?> <Br/> <gridview xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Id = "@ + ID/gridview" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: numcolumns = "auto_fit" <br/> Android: verticalspacing = "10dp" <br/> Android: horizontalspacing = "10dp" <br/> Android: columnwidth = "90dp" <br/> Android: stretchmode = "columnwidth" <br/> Android: gravity = "center" <br/>
Some attributes are described as follows:
Android: numcolumns = "auto_fit", set the number of columns in the gridview to automatic
Android: columnwidth = "90dp", the width of each column, that is, the width of the item
Android: stretchmode = "columnwidth", scaling and column width Synchronization
Android: verticalspacing = "10dp", the margin between two rows, for example, Row 1 (no.0 ~ No. 2) and row 2 (No. 3 ~ No. 5) 10dp spacing
Android: horizontalspacing = "10dp", margin between two columns.
Next we will introduceThe XML file is similar to the imageitem. xml file in the previous listview:
[XHTML] View plaincopyprint?
-
- <?XMLVersion="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>
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout <br/> xmns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_height = "wrap_content" <br/> Android: paddingbottom = "4dip" Android: layout_width = "fill_parent"> <br/> <imageview <br/> Android: layout_height = "wrap_content" <br/> Android: id = "@ + ID/itemimage" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_centerhorizontal = "true"> <br/> </imageview> <br/> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_below = "@ + ID/itemimage" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "textview01" <br/> Android: layout_centerhorizontal = "true" <br/> Android: id = "@ + ID/itemtext"> <br/> </textview> <br/> </relativelayout> <br/>
The final part is the source code of Java, which is similar to the Java source code of the previous listview, but the "selected" event processing is added:
[Java] View plaincopyprint?
-
- PublicVoidOncreate (bundle savedinstancestate ){
-
- Super. Oncreate (savedinstancestate );
-
- Setcontentview (R. layout. Main );
-
- Gridview = (gridview) findviewbyid (R. Id. gridview );
-
-
- // Generate dynamic array and transfer data
- Arraylist NewArraylist
-
- For(IntI =0; I <10; I ++)
-
- {
-
- Hashmap <string, Object> map =NewHashmap <string, Object> ();
-
- Map. Put ("Itemimage", R. drawable. Icon );// Add the image resource ID
- Map. Put ("Itemtext","No ."+ String. valueof (I ));// 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 =NewSimpleadapter (This,// No explanation
-
- Lstimageitem,// Data Source
-
- R. layout. night_item,// XML Implementation of night_item
-
-
- // Subitem of the dynamic array and imageitem
- NewString [] {"Itemimage","Itemtext"},
-
-
- // An imageview in the XML file of imageitem, two textview IDS
-
- NewInt[] {R. Id. itemimage, R. Id. itemtext });
-
- // Add and display
-
- Gridview. setadapter (saimageitems );
-
- // Add Message Processing
-
- Gridview. setonitemclicklistener (NewItemclicklistener ());
-
- }
-
- // When adapterview is clicked (touch screen or keyboard), the returned item Click Event
-
- ClassItemclicklistenerImplementsOnitemclicklistener
-
- {
-
- PublicVoidOnitemclick (adapterview <?> Arg0,// The adapterview where the click happened
-
- View arg1,// The view within the adapterview that was clicked
-
- IntArg2,// The Position of the view in the adapter
- LongArg3// The row ID of the item that was 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"));
-
- }
-
-
- }