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
-
public class ImageList extends Baseadapter {
-
-
public bitmap[] image;
-
Activity activity;
-
-
Construct
-
Public ImageList (Activity a) {
-
activity = A;
-
}
-
@Override
-
public int GetCount () {
-
TODO auto-generated Method Stub
-
return image.length;
-
}
-
@Override
-
Public Object getItem (int position) {
-
TODO auto-generated Method Stub
-
return image[position];
-
}
-
@Override
-
public long getitemid (int position) {
-
TODO auto-generated Method Stub
-
return position;
-
}
-
Public View GetView (int position, View Convertview, ViewGroup parent) {
-
TODO auto-generated Method Stub
-
ImageView ImageView;
-
if (Convertview = = null) {
-
ImageView = new ImageView (activity);
-
Imageview.setlayoutparams (New Gridview.layoutparams (50, 50));//Set ImageView width and height
-
Imageview.setadjustviewbounds (FALSE);
-
Imageview.setscaletype (ImageView.ScaleType.CENTER_CROP);
-
-
} else {
-
ImageView = (ImageView) Convertview;
-
}
-
Imageview.setimagebitmap (Image[position]);
-
-
return imageView;
-
}
-
public static Bitmap Drawabletobitmap (drawable drawable) {
-
-
Bitmap Bitmap = Bitmap
-
. CreateBitmap (
-
Drawable.getintrinsicwidth (),
-
Drawable.getintrinsicheight (),
-
Drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888
-
: Bitmap.Config.RGB_565);
-
Canvas canvas = new canvas (bitmap);
-
Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());
-
Drawable.draw (canvas);
-
return bitmap;
-
}
-
}
And then in the main program.
[C-sharp] View Plaincopy
-
Final GridView GV = (GridView) Findviewbyid (R.id.grid);
-
ImageList adapter = new ImageList (available.this);
-
Resources res=getresources ();
-
Bitmap Bmp=bitmapfactory.decoderesource (res, R.drawable.logo);
-
Adapter.image=new BITMAP[20];
-
for (int i=0;i<=19;i++)
-
{
-
Adapter.image[i]=bmp;
-
}
-
Gv.setadapter (adapter);
-
Gv.setonitemclicklistener (New Onitemclicklistener () {
-
@Override
-
public void Onitemclick (adapterview<?> arg0, View arg1,
-
int arg2, long arg3) {
-
TODO auto-generated Method Stub
-
LOG.I ("PHB", Arg2 + "");
-
LOG.I ("Phbsel", gv.isfocused () + "");
-
Intent i=new Intent (Available.this, Com.mobile.links.shop.mycx.shopcart.class);
-
StartActivity (i);
-
}
-
});
-
Gv.setonitemselectedlistener (New Onitemselectedlistener () {
-
@Override
-
public void onitemselected (adapterview<?> arg0, View arg1,
-
int arg2, long arg3) {
-
TODO auto-generated Method Stub
-
LOG.I ("Selected", arg2+ "");
-
Tv.settext (arg2+ ":d etail");
-
}
-
@Override
-
public void onnothingselected (adapterview<?> arg0) {
-
TODO auto-generated Method Stub
-
-
}
-
-
});
-
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
-
<?xml version= "1.0" encoding= "Utf-8"?>
-
<linearlayout
-
Android:id= "@+id/relativelayout"
-
Android:layout_width= "Fill_parent"
-
android:layout_height= "Fill_parent"
-
Xmlns:android= "Http://schemas.android.com/apk/res/android"
-
android:orientation= "Vertical"
-
>
-
<gridview android:id= "@+id/availgrid"
-
Android:layout_width= "Fill_parent"
-
android:layout_height= "Wrap_content"
-
Android:numcolumns= "4"
-
Android:verticalspacing= "30DP"
-
Android:layout_centerhorizontal = "true"
-
-
/>
-
-
-
</LinearLayout>
How to use the Android GridView