首页 > 代码库 > Android GridView的使用方法
Android GridView的使用方法
Android的GridView控件用于把一系列的空间组织成一个二维的网格显示出来 应用的比较多的就是组合图片显示 下面我就详细讲一个例子
首先写一个类继承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));// 设置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>
Android GridView的使用方法