RecycleView's setInitialPrefetchItemCount is not working
Asked Answered
D

2

5

I'm trying to improve the user experience while scrolling a grid of images by prefetching views ahead of time using the new API introduced in the support library 25.1 for controlling the number of prefetched items in a RecycleView.

GridLayoutManager layoutManager = new GridLayoutManager(this.getContext(), PhotoGridViewHolder.SPAN_SIZE);
layoutManager.setItemPrefetchEnabled(true);
layoutManager.setInitialPrefetchItemCount(30); 
photosRecycleView.setLayoutManager(layoutManager);
... add 100 photos to the adapter ...

And I also added logging for my ViewHolders so I could see if the binding really happened.

void bind(final Photo photo, int position) {
    // I expcet to see 30 logs without event scrolling as the intial set to 30.
    log.d("binding photo: " + photo.getId());
    loadPhoto(photo);
}

However, it seems like it's not working, as I loaded a list of 100 images but I see only a few log entries (just fetching the visible items), but I'd expect to see at 30 as I set it to be like so.

Dusen answered 26/10, 2017 at 15:40 Comment(0)
L
6

As @FlanschiFox and comments below his answer says, this is only working for inner RecyclerViews. Additionally, I wrote a PreloadLinearLayoutManager here to preload data 5 items before the list scrolls to the end. It may help you a bit.

Litre answered 20/7, 2018 at 8:17 Comment(5)
Thank you, that was really helpful.Slay
@HJWAJ, I checked your class and works when scrolling, but how do I preload the n items without scrolling?Branks
@makkhaygurung prefetch is designed to optimize scrolling performance. If you are using LinearLayoutManager, you can override LinearLayoutManager#getExtraLayoutSpace to preload items. Unfortunately I didn't find any way to preload with StaggeredGridLayoutManager.Litre
See Android Docs: "Calling this method does nothing unless the LayoutManager is in a RecyclerView nested in another RecyclerView", developer.android.com/reference/kotlin/androidx/recyclerview/…Indigent
@Litre You wonderful human, it worked!!! I'm on xamarin and your preload class worked for me after a little tweaking. thank you!Bouie
E
1

According to the docs, this is only working for inner RecyclerViews.

Documentation: isItemPrefetchEnabled

Sets the number of items to prefetch in collectInitialPrefetchPositions(int, LayoutPrefetchRegistry), which defines how many inner items should be prefetched when this LayoutManager's RecyclerView is nested inside another RecyclerView.

Evoke answered 26/10, 2017 at 15:57 Comment(4)
According to this article, it's not only for nested recycle views: medium.com/google-developers/recyclerview-prefetch-c2f269075710Dusen
The article says: Performing this work for nested RecyclerViews [..] is trickier ... This is the reason for the new API in v25.1 of LinearLayoutManager, setInitialItemPrefetchCount(), which tells the system how many items to prefetch to fill the RecyclerViewEvoke
Yes, I am aware of the nested recycler view cases. However, how many items are prefetched when using one of the default layout managers and a flat recycler view?Mervin
default is 2 @AvidProgrammerComposite

© 2022 - 2024 — McMap. All rights reserved.