Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecycleView's setInitialPrefetchItemCount is not working

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.

like image 267
Rom Shiri Avatar asked Oct 26 '17 15:10

Rom Shiri


People also ask

What is LinearLayoutManager Android?

LayoutManager implementations that lays out items in a grid. WearableLinearLayoutManager. This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout. A RecyclerView. LayoutManager implementation which provides similar functionality to ListView .

What is nested RecyclerView in Android?

In this tutorial you will learn step by step how to create a nested recyclerview. This is whereby you are placing a recyclerview within another recyclerview. Each recyclerview needs to have it's independent adapter to bind it's data.


2 Answers

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.

like image 103
HJWAJ Avatar answered Oct 12 '22 23:10

HJWAJ


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.

like image 24
foxy Avatar answered Oct 12 '22 23:10

foxy