Android gallery查看器回收
我根据教程制作了一个图库查看器,在单击缩略图时遇到了问题。 例如,如果我在第二个缩略图中单击,它将转到第三个,如果我在第四个缩略图中单击,它将转到第五个,以此类推。。但是如果我点击第六个,它实际上会转到第六个。 我想这一定与缩略图的位置和适配器的回收有关,但我无法找到解决方案 这是我的适配器:Android gallery查看器回收,android,adapter,image-gallery,android-gallery,Android,Adapter,Image Gallery,Android Gallery,我根据教程制作了一个图库查看器,在单击缩略图时遇到了问题。 例如,如果我在第二个缩略图中单击,它将转到第三个,如果我在第四个缩略图中单击,它将转到第五个,以此类推。。但是如果我点击第六个,它实际上会转到第六个。 我想这一定与缩略图的位置和适配器的回收有关,但我无法找到解决方案 这是我的适配器: public class ImageAdapter extends BaseAdapter { private Context ctx; int imageBackgro
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);
}
}
我想这一定是和政府的立场有关
缩略图和适配器的回收利用,但我不能陷入
解决办法
由于画廊不回收意见,我认为没有
ImageView iv;
if (arg1 == null) {
iv = new ImageView(ctx);
} else {
iv = (ImageView) arg1;
}
出于同样的原因,这只鹬也没用。您可以避免使用该控件,并每次分配
ImageView
,因为arg1将始终为空。您应该提供有关缓存位图的更多信息。我知道这个问题与缓存或mImagesUrls有关。是的,你是对的:我刚读到有一个bug使得View
参数总是null
。你是否使用HashMap来检索url,使用position作为键,使用HashMap来检索给定url的位图?我想是的,这种方法有什么不好的地方吗?有很多。然而,你的方法是正确的。可能问题在于在缓存中存储位图的方式。