Reputation: 1785
I'm starting to work with Glide to load images in android app but I have some questions about how it works and how it load the images, I hope you can explain me.
1.- For example, I have a grid view with 6 rows and 2 columns, but only 3 rows are visible until scroll, Glide loads all images even if they are not visible on screen? Or it loads images until the image until the ImageView is visible to the user?
2.- In case that loads all images on start, how can I make Glide load images only when they are visible to the user? Because I have a json with 30 images, but I don't want the user to spend a lot of it's data to load images if he is not scrolling to view more images.
I'm using this code to load each image of the grid:
public void onBindViewHolder(MyViewHolder holder, int position) {
GalleryPhotoObject image = images.get(position);
if (!Strings.isNullOrEmpty(image.getImage())) {
RequestOptions requestOptions = new RequestOptions();
requestOptions.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
Glide.with(context).load(Constants.BASE_URL + image.getImage())
.error(Glide.with(context).load(Constants.BASE_URL + image.getImage()))
.thumbnail(0.5f)
.apply(requestOptions)
.into(holder.thumbnail);
}
}
Upvotes: 1
Views: 626
Reputation: 4673
Glide loads the images exactly when you call
Glide.with(context).load(resource)
and take the width and height of theimage passed in
.into(imageview)
for caching the image with that width and height into local storage.
RecyclerViews as it's name tells, reuses or recycles the item views. So, even when you don't need to load any images you should call
Glide.with(context).clear(imageview)
in your binding, so old item images stops being displayed on new items when none image should be loaded.
Upvotes: 2
Reputation: 1373
In this case, Glide doesn't have anything to do with how many it should load. It's up to your implementation. The way you have setup loading Glide in onBindViewHolder
means that the image will be loaded every time a new View
is bound or in other words, when the View
appear on the screen.
Also, Glide has an internal cache as well so it doesn't load images that are already in it's cache.
One thing to note though is that, onBindViewHolder
isn't just called for only the images currently on the screen but also for images that are right above or below the currently shown one. That is to prepare for the incoming scroll so the user doesn't see empty views.
In your context, let's suppose that you have 3 visible rows then likely the onBindViewHolder
will be called for the 3 rows and possibly for 4th and 5th row which is the intended behaviour.
tldr: Your implementation is good right now to avoid extra and inefficient loading of images
Upvotes: 3