Android entry eighth of the GridView (nine chart)

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/

The GridView and ListView are often used as multi-control layouts, and the GridView is the first choice for implementing nine charts! This article is about how to use the GridView to implement the nine diagram. The use of the GridView method is very much, the most online introduction of the method is to achieve a imageadapter inheritance baseadapter, and then for the GridView use, similar to the method of this article no longer repeated, This article describes the use of the GridView method in front of the ListView in an extremely similar way .... Also is I lazy, hee hee hehe ....

First to post the results of this code execution:

This article needs to add/change 3 files: Main.xml, Night_item.xml, Java source code.

Main.xml source code such as the following, itself is a girdview, for loading item:

<?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 "a ndroid: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 number of columns of the GridView is set to its own initiative

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.

Next Introduction Night_item.xml, this XML is very similar to the Imageitem.xml in the previous listview:

<?xml version= "1.0" Encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" Andro id:layout_height= "Wrap_content" android:paddingbottom= "4dip" android:layout_width= "fill_parent" > <I Mageview android:layout_height= "wrap_content" android:id= "@+id/itemimage" and Roid: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></t extarea> </span>

The last is the Java source code, also with the previous ListView Java source code very similar, just more "selected" Event Processing:

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 arraylist<pashmap<string, object>> Lstimageitem = new arraylist<pashmap<string, O Bject>> (); for (int i=0;i<10;i++) {hashmap<string, object> map = new hashmap<string, object> (); Map.put ("Itemimage", r.drawable.icon);//Add the idmap.put of the Image resource ("Itemtext", "NO.") +string.valueof (i));//Itemtext Lstimageitem.add (map) by serial number; }//Generate adapter for Imageitem <====> dynamic array elements, both one by one corresponding simpleadapter Saimageitems = new Simpleadapter (this,//no explanation lstimageitem,//Data Source R.layout.night XML implementation of _item,//night_item//dynamic array with IMA GeitEM corresponding subkey new string[] {"Itemimage", "Itemtext"}, Imageitem an ImageView, two TextView ID in the XML file of a polygon New int[] {r.id.itemimage,r.id.itemtext}); Added and displayed gridview.setadapter (Saimageitems); Join the message Processing Gridview.setonitemclicklistener (new Itemclicklistener ()); }//When Adapterview is clicked (touchscreen or keyboard), the item returned is Click event class Itemclicklistener implements Onitemclicklistener {public voi D Onitemclick (adapterview<?> arg0,//the Adapterview where the click happened View arg1,//t He view within the Adapterview that is clicked Int arg2,//the position of the view in the Adapte R long arg3//the Row ID of the item that is clicked) {//In this example arg2=arg3h Ashmap<string, object> item= (hashmap<string, object>) arg0.getitematposition (ARG2);//Displays the Itemtextsettitle ((String) item.get ("Itemtext") of the selected item;} }

Android entry eighth of the GridView (nine chart)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.