Gridview for Android (jiugong figure)

Source: Internet
Author: User

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?
  1. <?XMLVersion="1.0"Encoding="UTF-8"?>
  2. <GridviewXmlns: 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. />

<? 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?
  1. <?XMLVersion="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>

<? 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?
  1. PublicVoidOncreate (bundle savedinstancestate ){
  2. Super. Oncreate (savedinstancestate );
  3. Setcontentview (R. layout. Main );
  4. Gridview = (gridview) findviewbyid (R. Id. gridview );
  5. // Generate dynamic array and transfer data
  6. Arraylist NewArraylist
  7. For(IntI =0; I <10; I ++)
  8. {
  9. Hashmap <string, Object> map =NewHashmap <string, Object> ();
  10. Map. Put ("Itemimage", R. drawable. Icon );// Add the image resource ID
  11. Map. Put ("Itemtext","No ."+ String. valueof (I ));// Itemtext by serial number
  12. Lstimageitem. Add (MAP );
  13. }
  14. // Generate the imageitem of the adapter <====> dynamic array elements, one-to-one correspondence between the two
  15. Simpleadapter saimageitems =NewSimpleadapter (This,// No explanation
  16. Lstimageitem,// Data Source
  17. R. layout. night_item,// XML Implementation of night_item
  18. // Subitem of the dynamic array and imageitem
  19. NewString [] {"Itemimage","Itemtext"},
  20. // An imageview in the XML file of imageitem, two textview IDS
  21. NewInt[] {R. Id. itemimage, R. Id. itemtext });
  22. // Add and display
  23. Gridview. setadapter (saimageitems );
  24. // Add Message Processing
  25. Gridview. setonitemclicklistener (NewItemclicklistener ());
  26. }
  27. // When adapterview is clicked (touch screen or keyboard), the returned item Click Event
  28. ClassItemclicklistenerImplementsOnitemclicklistener
  29. {
  30. PublicVoidOnitemclick (adapterview <?> Arg0,// The adapterview where the click happened
  31. View arg1,// The view within the adapterview that was clicked
  32. IntArg2,// The Position of the view in the adapter
  33. LongArg3// The row ID of the item that was 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. }
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.