Android entry eighth of the GridView (nine chart)

Source: Internet
Author: User

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?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <GridView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/gridview"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:numcolumns="Auto_fit"
  7. android:verticalspacing="10DP"
  8. android:horizontalspacing="10DP"
  9. android:columnwidth="90DP"
  10. android:stretchmode="ColumnWidth"
  11. android:gravity="center"
  12. />

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?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <relativelayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_height="wrap_content"
  5. android:paddingbottom="4dip" android:layout_width="fill_parent">
  6. <ImageView
  7. android:layout_height="wrap_content"
  8. android:id="@+id/itemimage"
  9. android:layout_width="wrap_content"
  10. android:layout_centerhorizontal="true">
  11. </ImageView>
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_below="@+id/itemimage"
  15. android:layout_height="wrap_content"
  16. android:text="TextView01"
  17. android:layout_centerhorizontal="true"
  18. android:id="@+id/itemtext">
  19. </TextView>
  20. </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?
    1. public void OnCreate (Bundle savedinstancestate) {
    2. super.oncreate (savedinstancestate);
    3. Setcontentview (R.layout.main);
    4. GridView GridView = (GridView) Findviewbyid (R.id.gridview);
    5. //Generate dynamic array and transfer to data
    6. arraylistnew arraylist
    7. For (int i=0;i<10;i++)
    8. {
    9. hashmap<string, object> map = new hashmap<string, object> ();
    10. Map.put ("Itemimage", R.drawable.icon); Add an ID for an image resource
    11. Map.put ("Itemtext", "NO." +string.valueof (i)); //Do Itemtext by serial number
    12. Lstimageitem.add (map);
    13. }
    14. //Generate adapter for Imageitem <====> dynamic array elements, both one by one corresponds
    15. Simpleadapter saimageitems = new Simpleadapter (this, //no explanation
    16. Lstimageitem,//Data source
    17. R.layout.night_item,XML implementation of//night_item
    18. //dynamic array with imageitem corresponding to the child
    19. New string[] {"itemimage","Itemtext"},
    20. //imageitem XML file inside of a imageview, two TextView ID
    21. new int[] {r.id.itemimage,r.id.itemtext});
    22. //Add and show
    23. Gridview.setadapter (Saimageitems);
    24. //Add message processing
    25. Gridview.setonitemclicklistener (new Itemclicklistener ());
    26. }
    27. //When Adapterview is clicked (touch screen or keyboard), the returned item click event
    28. class Itemclicklistener implements Onitemclicklistener
    29. {
    30. Public void Onitemclick (adapterview<?> arg0,//the adapterview where the click happened
    31. View Arg1,//the view within the Adapterview that is clicked
    32. int arg2,//the position of the view in the adapter
    33. long Arg3//the row ID of the item that is clicked
    34. ) {
    35. //In this example ARG2=ARG3
    36. Hashmap<string, object> item= (hashmap<string, object>) arg0.getitematposition (ARG2);
    37. //Display the Itemtext of the selected item
    38. Settitle (String) item.get ("Itemtext");
    39. }
    40. }

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.