Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android GridView的使用

Android GridView的使用

編輯:Android開發實例

Android的GridView控件用於把一系列的空間組織成一個二維的網格顯示出來應用的比較多的就是組合圖片顯示下面我就詳細講一個例子

首先寫一個類繼承BaseAdapter

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

 

然後在主程序裡

 

  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來做一些應用了。

構建gridview的xml代碼

 

  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. <GridView android:id="@+id/availgrid"   
  9.   android:layout_width="fill_parent"   
  10.   android:layout_height="wrap_content"   
  11.   android:numColumns="4"   
  12.   android:verticalSpacing="30dp"   
  13.   android:layout_centerHorizontal = "true" 
  14.     
  15.   />   
  16.    
  17.    
  18. </LinearLayout> 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved