大家好 -
我正在尝试实现一个显示图像ArrayList的画廊小部件,并从开发网站上的Hello, Gallery示例开始。这部分已经很好地实现了。
我需要让画廊显示一个空视图(当ArrayList没有内容时的特殊视图),但我似乎无法让画廊这样做。我过去曾在ListView和其他AdapterViews中这样做,但我无法让它在Gallery中工作。我需要在Adapter、Gallery或两者中重写/实现什么来显示空视图?这是我的适配器代码:
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private ArrayList<Drawable> images;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
images = new ArrayList<Drawable>();
}
public void addImage(Drawable d) {
images.add(d);
}
public boolean isEmpty() {
return getCount() == 0;
}
public int getCount() {
return images.size();
}
public Drawable getItem(int position) {
return images.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View contentView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageDrawable(images.get(position));
i.setLayoutParams(new Gallery.LayoutParams(160, 120));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
当使用空的ArrayList展示视图时,getCount()方法会被调用(返回0),但是Gallery从未检查isEmpty,当我在Gallery中定义了
getEmptyView()
时,它也从未被调用。我是否错过了BaseAdapter中另一个必需的方法以正确通知空状态?谢谢!