今天小研究了一下GridView,目的是为了实现 下面的效果(GridView多项选择):
首先,在布局文件添加GridView ,创建适配器的items...
具体的都在注释里边了,下面是 程序源码 片段,由于xml文件比较简单,故不在贴出.
MainActivity.java
package com.app.gridview; import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.GridView; public class MainActivity extends Activity {
static MainActivity instance;//实例
GridView gridView1;//声明对象
MyGridViewAdapter adapter;//声明适配器 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instance =this;
//获得组件
gridView1 =(GridView) findViewById(R.id.gridView1);
gridView1.setNumColumns(2);
//获得适配器
adapter = new MyGridViewAdapter(instance);
//设置适配器到组件
gridView1.setAdapter(adapter);
//添加监听
gridView1.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//更改显示
adapter.changeState(position);
}
});
}
}
自定义适配器 MyGridViewAdapter.java
package com.app.gridview; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView; public class MyGridViewAdapter extends BaseAdapter {
Context context;
boolean isCheck;// 选中
Boolean boo[] = {false, false, false, false, false}; int imgsid[] = {R.drawable.aa_badge_ad, R.drawable.aa_badge_award,
R.drawable.aa_badge_exclusive, R.drawable.aa_badge_gift,
R.drawable.aa_badge_new,};
public MyGridViewAdapter(Context context) {
this.context = context;
isCheck = false;
} @Override
public int getCount() {
return imgsid.length;
} @Override
public Object getItem(int position) {
return imgsid[position];
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));// 设置ImageView对象布局
imageView.setAdjustViewBounds(false);// 设置边界对齐
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);// 设置刻度的类型
imageView.setPadding(8, 8, 8, 8);// 设置间距
} else {
imageView = (ImageView) convertView;
}
imageView.setImageDrawable(makeBmp(imgsid[position], boo[position]));
// imageView.setImageResource(imgsid[position]);//为ImageView设置图片资源
return imageView;
} private LayerDrawable makeBmp(int id, boolean isChosen) {
Bitmap bitmap = ((BitmapDrawable) context.getResources()
.getDrawable(id)).getBitmap(); // 根据isChosen来选取对勾的图片
Bitmap seletedBmp;
if (isChosen == true) {
seletedBmp = BitmapFactory.decodeResource(context.getResources(),
R.drawable.btncheck_yes);
} else {
seletedBmp = BitmapFactory.decodeResource(context.getResources(),
R.drawable.btncheck_no);
} // 使 Drawable叠加层(合成图片)
Drawable[] array = new Drawable[2];
array[0] = new BitmapDrawable(bitmap);
array[1] = new BitmapDrawable(seletedBmp);
// 层图形对象
LayerDrawable mLayerDrawable = new LayerDrawable(array);// 参数为上面的Drawable数组
/*
* Specify modifiers to the bounds for the drawable[index].索引号 left += l
* top += t; right -= r; bottom -= b;
*/
mLayerDrawable.setLayerInset(0, 0, 0, 0, 0); // 第一个参数0代表数组的第一个元素
mLayerDrawable.setLayerInset(1, 88, 0, 0, 88);// 第一个参数1代表数组的第二个元素
return mLayerDrawable; // 返回合成后的图
}
/*
* 被点击的时候调用,改变点击后的状态
*/
public void changeState(int position) { if (isCheck == false) {
if (position < boo.length) {
boo[position] = !boo[position];
isCheck = !isCheck;
}
} else {
if (position < boo.length) {
boo[position] = !boo[position];
isCheck = !isCheck;
}
}
// 调用适配器的更新状态方法
notifyDataSetChanged();
}
}