简体   繁体   English

Android Gallery Viewer回收

[英]Android gallery viewer recycling

I made a gallery viewer based on this tutorial, and I'm having problems when clicking in the thumbnails. 我根据教程制作了一个图库查看器,单击缩略图时遇到了问题。 For example if I click in the 2nd thumbnail, it goes to the 3rd, if I click in the 4th, it goes to the 5th, etc.. but if I click on the 6th, it actually goes to the 6th. 例如,如果我单击第二个缩略图,则转到第三个,如果单击第四个,则转到第五个,依此类推。但是如果我单击第六个,则实际上转到第六个。 I presume it must have something to do with the position of the thumbnails and the recycling of the adapter, but I can't stumble into a solution. 我认为它一定与缩略图的位置和适配器的回收有关,但是我不能偶然发现一个解决方案。

This is my adapter: 这是我的适配器:

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;
        }
    }

Thanks a lot in advance! 在此先多谢!

EDIT: This is the caching I'm doing of the images for the thumbnails: 编辑:这是我正在为缩略图图像缓存:

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);
        }
    }

I presume it must have something to do with the position of the thumbnails and the recycling of the adapter, but I can't stumble into a solution. 我认为它一定与缩略图的位置和适配器的回收有关,但是我不能偶然发现一个解决方案。

Since gallery does not recycle views I think not. 由于图库不会回收视图,所以我认为不会。

            ImageView iv;
            if (arg1 == null) {
                iv = new ImageView(ctx);
            } else {
                iv = (ImageView) arg1;
            }

for the same reason this snipet is not useful. 出于同样的原因,此摘要没有用。 You can avoid the control and allocate the ImageView everytime, since arg1 will be always null. 您可以避免控件并每次分配ImageView ,因为arg1始终为null。 You should provide more information about you do cache your bitmap. 您应该提供有关缓存位图的更多信息。 IMHO the issue is cache or mImagesUrls related. 恕我直言,问题与缓存或mImagesUrls有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM