How to use the Android GridView

Source: Internet
Author: User

The GridView control for Android is used to organize a series of spaces into a two-dimensional grid showing the application of the more is the combination of picture display below I will elaborate an example

First write a class inheritance Baseadapter

[Java] view plaincopy

  1. public class ImageList extends Baseadapter {

  2. public bitmap[] image;

  3. Activity activity;

  4. Construct

  5. Public ImageList (Activity a) {

  6. activity = A;

  7. }

  8. @Override

  9. public int GetCount () {

  10. TODO auto-generated Method Stub

  11. return image.length;

  12. }

  13. @Override

  14. Public Object getItem (int position) {

  15. TODO auto-generated Method Stub

  16. return image[position];

  17. }

  18. @Override

  19. public long getitemid (int position) {

  20. TODO auto-generated Method Stub

  21. return position;

  22. }

  23. Public View GetView (int position, View Convertview, ViewGroup parent) {

  24. TODO auto-generated Method Stub

  25. ImageView ImageView;

  26. if (Convertview = = null) {

  27. ImageView = new ImageView (activity);

  28. Imageview.setlayoutparams (New Gridview.layoutparams (50, 50));//Set ImageView width and height

  29. Imageview.setadjustviewbounds (FALSE);

  30. Imageview.setscaletype (ImageView.ScaleType.CENTER_CROP);

  31. } else {

  32. ImageView = (ImageView) Convertview;

  33. }

  34. Imageview.setimagebitmap (Image[position]);

  35. return imageView;

  36. }

  37. public static Bitmap Drawabletobitmap (drawable drawable) {

  38. Bitmap Bitmap = Bitmap

  39. . CreateBitmap (

  40. Drawable.getintrinsicwidth (),

  41. Drawable.getintrinsicheight (),

  42. Drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888

  43. : Bitmap.Config.RGB_565);

  44. Canvas canvas = new canvas (bitmap);

  45. Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());

  46. Drawable.draw (canvas);

  47. return bitmap;

  48. }

  49. }

And then in the main program.

[C-sharp] View Plaincopy

  1. Final GridView GV = (GridView) Findviewbyid (R.id.grid);

  2. ImageList adapter = new ImageList (available.this);

  3. Resources res=getresources ();

  4. Bitmap Bmp=bitmapfactory.decoderesource (res, R.drawable.logo);

  5. Adapter.image=new BITMAP[20];

  6. for (int i=0;i<=19;i++)

  7. {

  8. Adapter.image[i]=bmp;

  9. }

  10. Gv.setadapter (adapter);

  11. Gv.setonitemclicklistener (New Onitemclicklistener () {

  12. @Override

  13. public void Onitemclick (adapterview<?> arg0, View arg1,

  14. int arg2, long arg3) {

  15. TODO auto-generated Method Stub

  16. LOG.I ("PHB", Arg2 + "");

  17. LOG.I ("Phbsel", gv.isfocused () + "");

  18. Intent i=new Intent (Available.this, Com.mobile.links.shop.mycx.shopcart.class);

  19. StartActivity (i);

  20. }

  21. });

  22. Gv.setonitemselectedlistener (New Onitemselectedlistener () {

  23. @Override

  24. public void onitemselected (adapterview<?> arg0, View arg1,

  25. int arg2, long arg3) {

  26. TODO auto-generated Method Stub

  27. LOG.I ("Selected", arg2+ "");

  28. Tv.settext (arg2+ ":d etail");

  29. }

  30. @Override

  31. public void onnothingselected (adapterview<?> arg0) {

  32. TODO auto-generated Method Stub

  33. }

  34. });

Where the onclick function is judged to be clicked

And the Onselect function is the ImageView that determines the focus that is chosen.

So that we can do some applications based on the GridView.

Building the XML code for the GridView

[C-sharp] View Plaincopy

  1. <?xml version= "1.0" encoding= "Utf-8"?>

  2. <linearlayout

  3. Android:id= "@+id/relativelayout"

  4. Android:layout_width= "Fill_parent"

  5. android:layout_height= "Fill_parent"

  6. Xmlns:android= "Http://schemas.android.com/apk/res/android"

  7. android:orientation= "Vertical"

  8. >

  9. <gridview android:id= "@+id/availgrid"

  10. Android:layout_width= "Fill_parent"

  11. android:layout_height= "Wrap_content"

  12. Android:numcolumns= "4"

  13. Android:verticalspacing= "30DP"

  14. Android:layout_centerhorizontal = "true"

  15. />

  16. </LinearLayout>

How to use the Android GridView

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.