Actually I'm trying to load images into my reycycleview
using glide 4.4.0
and it's loading fine . But now the problem is my recyclerview
is lagging a bit when I scroll fast due to the image loading . But I saw some of the glide's method called preload
and downloadOnly
.So,my question is are these methods helpful for loading image in advance if so then how to use them?
Asked
Active
Viewed 3,762 times
4

Zoe
- 27,060
- 21
- 118
- 148

Srinivas Nahak
- 1,846
- 4
- 20
- 45
-
1have look [this](https://stackoverflow.com/a/38219830/5110595) – Hemant Parmar Mar 07 '18 at 09:50
-
@HemantParmar thanks for your time sir I'll try to use it – Srinivas Nahak Mar 07 '18 at 10:21
-
I had this kind of issue, not in a list but the preload one https://stackoverflow.com/questions/37964187/preload-multiple-images-with-glide – An-droid Mar 07 '18 at 14:59
1 Answers
2
You can use this to fine-tune your preloading. It requires quite a bit of work but looks promising.
In short, you need to create a PreLoader class that will get image URLs from your data set:
private class MyPreloadModelProvider implements PreloadModelProvider {
@Override
@NonNull
public List<U> getPreloadItems(int position) {
String url = myUrls.get(position);
if (TextUtils.isEmpty(url)) {
return Collections.emptyList();
}
return Collections.singletonList(url);
}
@Override
@Nullable
public RequestBuilder getPreloadRequestBuilder(String url) {
return
GlideApp.with(fragment)
.load(url)
.override(imageWidthPixels, imageHeightPixels);
}
}
And then you set it as a scroll listener on your recycler view:
PreloadSizeProvider sizeProvider =
new FixedPreloadSizeProvider(imageWidthPixels, imageHeightPixels);
PreloadModelProvider modelProvider = new MyPreloadModelProvider();
RecyclerViewPreloader<Photo> preloader =
new RecyclerViewPreloader<>(
Glide.with(this), modelProvider, sizeProvider, 10 /*maxPreload*/);
RecyclerView myRecyclerView = (RecyclerView) result.findViewById(R.id.recycler_view);
myRecyclerView.addOnScrollListener(preloader);
After this, you'll get your images preloaded before the onBondViewHolder callback in the adapter, and you'll be able to display them from the cache.
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
ImageView imageView = ((MyViewHolder) viewHolder).imageView;
String currentUrl = myUrls.get(position);
GlideApp.with(fragment)
.load(currentUrl)
.override(imageWidthPixels, imageHeightPixels)
.into(imageView);
}

TpoM6oH
- 8,385
- 3
- 40
- 72
-
Thanks a lot for your suggestion sir , I'll inform you about it's performance after implementing it – Srinivas Nahak Mar 07 '18 at 10:20
-
Do you know if there is some way of using the recycler integration with other than LinearLayoutManager? – Granjero Apr 09 '19 at 19:14