我使用PageKeyedDataSource来实现无限滚动列表。它起作用了。它看起来像这样(简化版):
class ProfilePostDataSource: PageKeyedDataSource<Pair<Long, Long>, ProfilePost> {
fun loadInitial(... ...) {
val list = retrofitClient.getSomeData();
callback.onResult(list, null, next);
}
fun loadBefore(...) {}
fun loadAfter(...) {
val pageNumber = params.key;
val next = pageNumber + 1;
Log.e(TAG, "onLoadAfter -> "+pageNumber); // <---------- tells me in the log which page is loading
val list = retrofitClient.getSomeData(pageNumber);
callback.onResult(list, next);
}
}
//1
val config: PagedList.Config = PagedList.Config.Builder()
.setPageSize(15) // Don't google. Hit ctrl+b and read doc. Be happy
.setPrefetchDistance(1) // Don't google. Hit ctrl+b and read doc. Be happy
.setMaxSize(32) // Don't google. Hit ctrl+b and read doc. Be happy
.setEnablePlaceholders(false) // true -> show a dummy placeholder for unloaded items. false -> dont show
.build();
loadAfter()
太快了。所以在我的日志里,我看到了:
onLoadAfter -> 2
onLoadAfter -> 3
onLoadAfter -> 4
onLoadAfter -> 5 // 5 / 6 loadAfter are called
所以当我完成我的清单,下一个
loadAfter
被称为。
onLoadAfter ->6 // only one loadAfter is called. Subsequently, when I scroll to the end, the next loadAfter is called