我已基于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
}
}
}
答案 0 :(得分:0)
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()
您可以查看完整答案here