Search code examples
androidimageviewuniversal-image-loader

Universal Image Loader - Use same image in multiple views


I'm using Universal Image Loader to load images from a backend to display user images in a list; however, if the icon shows up multiple times, the Universal Image Loader doesn't fill out all the views.

  • [User Image 1] - No image
  • [User Image 1] - No Image
  • [User Image 2] - Fine
  • [User Image 2] - No Image
  • [User Image 3] - Fine
  • [User Image 1] - Fine

And then on another screen:

  • [User Image 1] - Fine
  • [User Image 1] - No image

I'm using cacheInMemory and cacheOnDisk, which seemed to improve it. As before it was only displaying it in one of the views, instead of most, but I need all of them to work.

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheInMemory( true )
            .cacheOnDisk( true )
            .build();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( this )
            .threadPoolSize( 3 )
            .defaultDisplayImageOptions( defaultOptions )
            .build();

ImageLoader.getInstance().init( config );

I'm not using a ListView for this task, I'm using a ScrollView and inflating it with a custom layout.

private View createSmallActivity( LayoutInflater inflater, final Event activity ) {

    final View view;
    view = inflater.inflate( R.layout.activity_posted_small, null );
    ...
    // The owner's image.
    if( activity.ownerImageUrl != null ) {
        Loader.loadImage( getActivity(),
               activity.ownerImageUrl,
               R.drawable.postedactivitysmall_imageprofileempty,
               ( ImageView ) view.findViewById( R.id.profileImage ) );
    }
    return view;
}



// Loader.loadImage
// Setting the targetSize, and masking the image with a resource.
public static void loadImage( Context context, String url, int resource, ImageView view ) {
    Drawable d = context.getResources().getDrawable( resource );
    int h = d.getIntrinsicHeight();
    int w = d.getIntrinsicWidth();

    ImageSize targetSize = new ImageSize( w, h );
    ImageLoader.getInstance().loadImage( url, targetSize, new MaskImageLoader( context, view, resource ) );
}

Any idea on how I can improve the Universal Image Loader to ensure all the views are correctly filled out?

Thanks for your help!


Solution

  • It's because Universal Image Loader cancel previous requests with same url (used as id). To prevent this behaviour, replace

    ImageLoader.getInstance().loadImage( url, targetSize, new MaskImageLoader( context, view, resource ) );
    

    by

    ImageLoader.getInstance().displayImage(url, 
        new NonViewAware(new ImageSize(w, h), ViewScaleType.CROP),
        new MaskImageLoader(context, view, resource));