程序员人生 网站导航

[置顶] Android GridView的使用方法

栏目:互联网时间:2014-11-11 08:40:37

Android的GridView控件用于把1系列的空间组织成1个2维的网格显示出来 利用的比较多的就是组合图片显示 下面我就详细讲1个例子

首先写1个类继承BaseAdapter

[java] view plaincopy

  1. public class ImageList extends BaseAdapter {  

  2.       

  3.         public Bitmap[] image;  

  4.         Activity activity;  

  5.           

  6.         // construct  

  7.         public ImageList(Activity a) {  

  8.             activity = a;  

  9.         }  

  10.         @Override  

  11.         public int getCount() {  

  12.             // TODO Auto-generated method stub  

  13.             return image.length;  

  14.         }  

  15.         @Override  

  16.         public Object getItem(int position) {  

  17.             // TODO Auto-generated method stub  

  18.             return image[position];  

  19.         }  

  20.         @Override  

  21.         public long getItemId(int position) {  

  22.             // TODO Auto-generated method stub  

  23.             return position;  

  24.         }  

  25.         public View getView(int position, View convertView, ViewGroup parent) {  

  26.             // TODO Auto-generated method stub  

  27.             ImageView imageView;  

  28.             if (convertView == null) {  

  29.                 imageView = new ImageView(activity);  

  30.                 imageView.setLayoutParams(new GridView.LayoutParams(50, 50));// 设置ImageView宽高  

  31.                 imageView.setAdjustViewBounds(false);  

  32.                 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);  

  33.                   

  34.             } else {  

  35.                 imageView = (ImageView) convertView;  

  36.             }  

  37.             imageView.setImageBitmap(image[position]);  

  38.               

  39.             return imageView;  

  40.         }  

  41.         public static Bitmap drawableToBitmap(Drawable drawable) {    

  42.                 

  43.             Bitmap bitmap = Bitmap    

  44.                             .createBitmap(    

  45.                                             drawable.getIntrinsicWidth(),    

  46.                                             drawable.getIntrinsicHeight(),    

  47.                                             drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888    

  48.                                                             : Bitmap.Config.RGB_565);    

  49.             Canvas canvas = new Canvas(bitmap);    

  50.             drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());    

  51.             drawable.draw(canvas);    

  52.             return bitmap;    

  53.     }    

  54.     }  

然后在主程序里

[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+":detail");  

  29.                 }  

  30.                 @Override  

  31.                 public void onNothingSelected(AdapterView<?> arg0) {  

  32.                     // TODO Auto-generated method stub  

  33.                       

  34.                 }  

  35.                      

  36.                });  

  37.                   

其中onClick函数是判断被点击

而onSelect函数是判断被选中 也就是取得焦点的imageview

这样我们就能够基于gridview来做1些利用了。

构建gridview的xml代码

[c-sharp] view plaincopy

  1. <?xml version="1.0" encoding="utf⑻"?>  

  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.   />   

  17.    

  18.    

  19. </LinearLayout>  

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐