Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Paging library doesn't fire loadAfter()

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

  • index + 9 (last item with a page size of 10 item)
  • prefetchDistance is 1
  • leadingNullCount is always 0 (not sure if I understand this attribute)
  • storageCount is the pageSize I configured above (according to the source code of this class)

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.

like image 677
Rafi Panoyan Avatar asked Dec 29 '17 10:12

Rafi Panoyan


1 Answers

Replace ListAdapter with PagedListAdapter

like image 111
Dmytro Batyuk Avatar answered Oct 24 '22 08:10

Dmytro Batyuk