Android 分页库不会触发 loadAfter()
Android Paging library doesn't fire loadAfter()
我正在使用新的 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(根据这个class的源码)
给予
int appendItems = 9 + 1 (0 - 10)
基于此,scheduleAppend() 永远不会被调用,因为 mAppendItemsRequested 也始终为 0。
我发现在滚动上实际加载更多数据的唯一方法是将我的 prefetchDistance 设置为任何大于 1 的值。话虽如此,这似乎更像是一种解决方法,而不是解决这个问题的好方法。
用 PagedListAdapter 替换 ListAdapter
我正在使用新的 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(根据这个class的源码)
给予
int appendItems = 9 + 1 (0 - 10)
基于此,scheduleAppend() 永远不会被调用,因为 mAppendItemsRequested 也始终为 0。
我发现在滚动上实际加载更多数据的唯一方法是将我的 prefetchDistance 设置为任何大于 1 的值。话虽如此,这似乎更像是一种解决方法,而不是解决这个问题的好方法。
用 PagedListAdapter 替换 ListAdapter