简体   繁体   English

Android ListView适配器异步任务图像加载

[英]android listview adapter asynctask image load

The problem that I'm getting is: 我得到的问题是:

When I scroll the listview BEFORE an item's (say x) imageview is loaded, then after scrolling, that item's (x) image gets loaded and displayed in some other item's (say y) imageview for short amount of time until that item (y) imageview is not loaded. 当我在加载项目(例如x)的imageview之前滚动列表视图时,然后在滚动后,该项目的(x)图像被加载并在其他项目(例如y)的imageview中显示很短的时间,直到该项目(y) imageview未加载。

I'm using Android Universal Image Loader library. 我正在使用Android Universal Image Loader库。 I have the following asynctask in bindView of my listview cursor adapter : 我的listview 游标适配器的 bindView中有以下asynctask:

@Override
public void bindView(View view, Context context, Cursor cursor) {
...
    ImageView imageView = (ImageView) view.findViewById(R.id.icon);
    new DownloadImageAsyncTask(url, imageView).execute();
...
}

private class DownloadImageAsyncTask extends AsyncTask<Void, Void, Bitmap> {

        String image_link;
        ImageView imageView;
        final WeakReference<ImageView> viewReference;

        public DownloadImageAsyncTask(String image_link, ImageView imageView) {
            this.image_link = image_link;
            this.imageView = imageView;
            viewReference = new WeakReference<ImageView>( imageView );
        }

        @Override
        protected Bitmap doInBackground(Void... params) {
            ... // downloading bitmap here
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {

            ImageView imageView = viewReference.get();
            if( imageView != null ) {

                ImageLoader imageLoader = ImageLoader.getInstance();

                DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .cacheOnDisk(true).cacheInMemory(true)
                        .showImageOnFail(R.drawable.ic_contact_picture)
                        .displayer(new FadeInBitmapDisplayer(300)).build();

                imageLoader.displayImage(image_link, imageView, options); // new options will be used
            }    
        }
}

Why does image that should be in item x get displayed for short amount of time in item y (when scrolling before item x image is loaded) ? 为什么应该在项y中短时间显示应在项x中显示的图像(在加载项x图像之前滚动时)?

Is internal recycling of listview items causing a problem? 内部回收listview项是否引起问题?

The problem seemed to be with Android Universal Image Loader library. 问题似乎出在Android Universal Image Loader库上。 I just moved onto Picasso. 我刚搬到毕加索。

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

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