Android Paging库不会触发loadAfter()

Raf*_*yan 23 pagination android android-recyclerview android-architecture-components

我正在使用新的Android Paging库来获得具有无限滚动功能的RecyclerView.我无法理解为什么当我像这样设置我的PagedList时,库不会触发loadAfter()方法:

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()
Run Code Online (Sandbox Code Playgroud)

注意:我的prefetchDistance是1,因为我想在看到最后一个项目时加载另一批数据.文档说0将永远不会加载更多数据

我的DataSource是一个PageKeyedDataSource,键是页面的索引.

我已经调查的源代码ContiguousPagedList这是当你使用一个PageKeyedDataSource,我无法理解这些所产生的特定PagedList:

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

根据我的配置,当看到我的最后一项时,appendItems值为0

int appendItems = index + mConfig.prefetchDistance - (mStorage.getLeadingNullCount()+ mStorage.getStorageCount());

  • index + 9(页面大小为10项的最后一项)
  • prefetchDistance是1
  • leadingNullCount始终为0(不确定我是否理解此属性)
  • storageCount是我在上面配置的pageSize(根据此类的源代码)

int appendItems = 9 + 1(0 - 10)

基于此,从不调用scheduleAppend(),因为mAppendItemsRequested也始终为0.

我发现在滚动上实际加载更多数据的唯一方法是将我的prefetchDistance设置为优于1的任何值.这就是说,它似乎更像是一个解决方法,而不是这个问题的好答案.

Dmy*_*yuk 5

将 ListAdapter 替换为 PagedListAdapter