Get started with Android: GridView (jiugong figure), androidgridview
This article from http://blog.csdn.net/hellogv/
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 ....
First, paste the code running result of this article:
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?
- <? 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"
- />
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 introduce the content of "maid", which is similar to the ImageItem. XML of 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 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?
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- 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. night_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
- HashMap <String, Object> item = (HashMap <String, Object>) arg0.getItemAtPosition (arg2 );
- // Display the ItemText of the selected Item
- SetTitle (String) item. get ("ItemText "));
- }
- }
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.