I'm using the new Android Paging library to get a RecyclerView with infinite scrolling. I cannot understand why the library does not fire the loadAfter() method when I setup my PagedList like this:
val config: PagedList.Config = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(10)
.setPrefetchDistance(1)
.setInitialLoadSizeHint(10)
.build()
val pageList = PagedList.Builder(LookDataSource(lookList), config)
.setInitialKey(0)
.setMainThreadExecutor(MainThreadExecutor())
.setBackgroundThreadExecutor(PaginationThreadPoolExecutor())
.setBoundaryCallback(LookBoundaryCallback())
.build()
Note: my prefetchDistance is 1, since I want to load another batch of data when my last item is seen. The documentation says that 0 would be to never load more data
My DataSource is a PageKeyedDataSource, and the key is the index of the page.
I have looked into the source code of ContiguousPagedList which is the particular PagedList created when you use a PageKeyedDataSource and I cannot understand these :
@MainThread
@Override
protected void loadAroundInternal(int index) {
int prependItems = mConfig.prefetchDistance - (index - mStorage.getLeadingNullCount());
int appendItems = index + mConfig.prefetchDistance
- (mStorage.getLeadingNullCount() + mStorage.getStorageCount());
mPrependItemsRequested = Math.max(prependItems, mPrependItemsRequested);
if (mPrependItemsRequested > 0) {
schedulePrepend();
}
mAppendItemsRequested = Math.max(appendItems, mAppendItemsRequested);
if (mAppendItemsRequested > 0) {
scheduleAppend();
}
}
According to my configuration, the appendItems value is 0 when my last item is seen
int appendItems = index + mConfig.prefetchDistance - (mStorage.getLeadingNullCount() + mStorage.getStorageCount());
with
gives
int appendItems = 9 + 1 (0 - 10)
Based on this, the scheduleAppend() is never called since mAppendItemsRequested is always 0 too.
The only way I found to actually load more data on scroll is to set my prefetchDistance to any value superior to 1. That being said, it seems more like a workaround than the good answer to this problem.
Replace ListAdapter with PagedListAdapter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With