Glide loading images each time when scrolling recyclerview

Prithniraj Nicyone picture Prithniraj Nicyone · Jan 25, 2016 · Viewed 8.4k times · Source

I have a RecyclerView and each item of RecyclerView is having ImageView. I am loading the image in that ImageView using Glide, when I scroll down the RecyclerView it loads images and this is fine, but when I again srcoll up the RecyclerView it again loads those images which are already been loaded. I do not want to load that images again which have already been loaded.

I am using the below code to load images using Glide

Glide.with(mActivity)
                .load(img.getmGridViewImageUrl())
                .into(imageHolder.imageView);

Answer

Logic picture Logic · Jan 25, 2016

You can cache the images either on disk or in Memory.

By default images are cached in Memory by Glide

It's also good to know that Glide will put all image resources into the memory cache by default. Thus, a specific call .skipMemoryCache( false ) is not necessary.

If you want to enable cache in disk you can use one of the following

  1. DiskCacheStrategy.SOURCE caches only the original full-resolution image.
  2. DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution (and possibly transformations)
  3. DiskCacheStrategy.ALL caches all versions of the image (default behavior)

Usage :

Glide  
    .with( context )
    .load( url )
    .diskCacheStrategy( DiskCacheStrategy.ALL )
    .into( imageViewInternet );