[英]Android GridView Adapter
When creating a Adapter for a GridView, like following : 为GridView创建适配器时,如下所示:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
}
How does Adapter know how many items it has to display on screen? 适配器如何知道它必须在屏幕上显示多少个项目? What if I want several items to display the same picture ?
如果我要多个项目显示同一张图片怎么办? Thanks.
谢谢。
Whatever count the getCount method returns is the number of items adapter is going to display getCount方法返回的计数是适配器将显示的项目数
public int getCount() {
return mThumbIds.length;
}
If you want several items to display same image create a different array of images, having some repeated images, and return length of that array. 如果要让多个项目显示同一图像,请创建一个不同的图像数组,并包含一些重复的图像,然后返回该数组的长度。
for eg: 例如:
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_5, R.drawable.sample_5
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_7, R.drawable.sample_7
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.