Android的GridView控件用於把一系列的空間組織成一個二維的網格顯示出來 應用的比較多的就是組合圖片顯示 下面我就詳細講一個例子 java
首先寫一個類繼承BaseAdapter android
[java] view plaincopy canvas
public class ImageList extends BaseAdapter { ide
public Bitmap[] image; 函數
Activity activity; this
// construct spa
public ImageList(Activity a) { code
activity = a; orm
} xml
@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));// 設置ImageView寬高
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;
}
}
而後在主程序裏
[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+":detail");
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
其中onClick函數是判斷被點擊
而onSelect函數是判斷被選中 也就是得到焦點的imageview
這樣咱們就能夠基於gridview來作一些應用了。
構建gridview的xml代碼
[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>