我根据本教程制作了一个画廊查看器,但在单击缩略图时遇到了问题。例如,如果我点击第二个缩略图,它会转到第三个,如果我点击第四个,它会转到第五个等等。但是如果我点击第六个,它实际上会转到第六个。我认为它必须与缩略图的位置和适配器的回收有关,但我无法偶然找到解决方案。
这是我的适配器:
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
public int getCount() {
return mImagesUrls.size();
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
// Thumbnails gallery
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv;
if (arg1 == null) {
iv = new ImageView(ctx);
} else {
iv = (ImageView) arg1;
}
// iv.setImageResource(mImagesUrls.get(arg0));
mCache.loadBitmap(mImagesUrls.get(arg0), iv);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
// Image size of the thumbnails
iv.setLayoutParams(new Gallery.LayoutParams(200, 120));
iv.setBackgroundResource(imageBackground);
GalleryTitleTv.setText((arg0 + 1) + " van " + mImagesUrls.size());
return iv;
}
}
提前非常感谢!
编辑:这是我对缩略图图像进行的缓存:
public void loadBitmap(String url, ImageView imageView) {
Bitmap mImageHolder = BitmapFactory.decodeResource(context.getResources(), R.drawable.pic_2);
final String imageKey = url;
final Bitmap bitmap = get(imageKey);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
imageView.setImageBitmap(mImageHolder);
BitmapWorkerTask task = new BitmapWorkerTask(imageView);
task.execute(url);
}
}