我已经实施了 分页功能 基于 PagedListAdapter
我已经检查过,工作正常。我试着将其实现为 如果ListAdapter项目为空,则显示EmptyHolder。. 我已经试过了,根据 commitCallback
后称 submitList
如下代码。
为什么PagedListAdapter项目计数显示为0,即使响应包含项目?如何解决这个问题?
private suspend fun loadItems(beforeId: String) = withContext(Dispatchers.IO) {
getFooUseCase(beforeId)
}
override fun loadInitial(
params: LoadInitialParams<String>,
callback: LoadInitialCallback<String, Foo>
) {
launch {
loadItems(null)
.run {
Timber.d("loadInitial : ${list.size}") // onResponse : 20
callback.onResult(list, null, beforeId)
}
}
}
observe(fooList) { items ->
// items.size or loadedCount always 0
fooAdapter.submitList(items) {
//fooAdapter.itemCount, fooAdapter.currentList.size or loadedSize
//always 0...
if(fooAdapter.itemCount == 0) {
//show EmptyHolder
}
}
}
PagedList.BoundaryCallback
有 onZeroItemsLoaded
处理空初始负载的方法
val livedPageList = LivePagedListBuilder(sourceFactory, config)
.setBoundaryCallback(object: PagedList.BoundaryCallback<YourItem>() {
override fun onZeroItemsLoaded() {
super.onZeroItemsLoaded()
// Handle empty initial load here
}
override fun onItemAtEndLoaded(itemAtEnd: YourItem) {
super.onItemAtEndLoaded(itemAtEnd)
// Here you can listen to last item on list
}
override fun onItemAtFrontLoaded(itemAtFront: YourItem) {
super.onItemAtFrontLoaded(itemAtFront)
// Here you can listen to first item on list
}
})
.build()
您可以查看完整的答案 此处