自定义适配器

public class MainActivity extends ActionBarActivity {
private GridView gv;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv=(GridView)findViewById(R.id.gridView11);
gv.setAdapter(new ImageAdapter(this));

}
//自定义设配器类
class ImageAdapter extends BaseAdapter{
//定义内容数组,要显示的图片资源
private int[] images={
R.drawable.bg01,R.drawable.bg02,R.drawable.bg03,
R.drawable.bg04,R.drawable.bg05,R.drawable.bg06,
R.drawable.bg07,R.drawable.bg08,R.drawable.bg09,
R.drawable.bg01,R.drawable.bg02,R.drawable.bg03,
R.drawable.bg04,R.drawable.bg05,R.drawable.bg06,
R.drawable.bg07,R.drawable.bg08,R.drawable.bg09
};

private Context c;
public ImageAdapter(Context myContext){
this.c=myContext;

}
@Override  
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}


@Override
public Object getItem(int position) {
return position;
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
 
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ImageView imageView;
if(convertView==null){
imageView=new ImageView(c);
//设置imageView视图的的宽高
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
//设置图片的拉伸方式(截取方式)
imageView.setScaleType(ScaleType.CENTER_CROP);
//设置边距

}else{
imageView=(ImageView)convertView;

}
imageView.setImageResource(images[position]);
return imageView;
}


}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值