Android Paging库不会触发loadAfter()

时间:2017-12-29 10:12:57

标签: android android-recyclerview pagination 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()

注意:我的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();
    }
}

根据我的配置,当看到我的上一个项目时, 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的任何值。尽管如此,它看起来更像是一个解决方法,而不是解决此问题的好方法。

1 个答案:

答案 0 :(得分:1)

用PagedListAdapter替换ListAdapter