Android Paging库不会触发loadAfter() [英] Android Paging library doesn't fire loadAfter()
问题描述
我正在使用新的Android Paging库获取具有无限滚动的RecyclerView.我不明白为什么当我这样设置PagedList时,库不触发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()
注意:我的prefetchDistance为1,因为我想在看到最后一个项目时加载另一批数据.文档说0表示永远不会加载更多数据
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
我的数据源是 PageKeyedDataSource ,键是页面的索引.
My DataSource is a PageKeyedDataSource, and the key is the index of the page.
我研究了 ContiguousPagedList 的源代码,这是您使用PageKeyedDataSource时创建的特定PagedList,但我听不懂这些内容:
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();
}
}
根据我的配置,当我看到最后一个项目时, appendItems 值为0
According to my configuration, the appendItems value is 0 when my last item is seen
int appendItems =索引+ mConfig.prefetchDistance -(mStorage.getLeadingNullCount()+ mStorage.getStorageCount());
int appendItems = index + mConfig.prefetchDistance - (mStorage.getLeadingNullCount() + mStorage.getStorageCount());
与
- 索引+ 9(页面大小为10的最后一项)
- prefetchDistance为1
- leadingNullCount始终为0(不确定我是否了解此属性)
- storageCount是我在上面配置的pageSize(根据此类的源代码)
给予
int appendItems = 9 + 1(0-10)
int appendItems = 9 + 1 (0 - 10)
基于此, scheduleAppend()不会被调用,因为 mAppendItemsRequested 始终也为0.
Based on this, the scheduleAppend() is never called since mAppendItemsRequested is always 0 too.
我发现在滚动上实际加载更多数据的唯一方法是将我的prefetchDistance设置为大于1的任何值.话虽如此,它似乎比解决此问题的方法要好得多.
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.
推荐答案
用PagedListAdapter替换ListAdapter
Replace ListAdapter with PagedListAdapter
这篇关于Android Paging库不会触发loadAfter()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!