gpt4 book ai didi

android - 在 submitList 之后没有得到 PagedListAdapter 的 itemCount

转载 作者:行者123 更新时间:2023-12-05 00:05:44 25 4
gpt4 key购买 nike

我基于 PagedListAdapter 实现了分页功能,并且我已经过检查工作正常。我尝试实现以如果 ListAdapter 项为空则显示 EmptyHolder 为特色。在调用 submitList 之后,我已经尝试基于 commitCallback 如下代码。

问题

Why has PagedListAdapter item count shows as 0 even though the response contains items? How to solve this problem?

FooDataSource

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)
}
}
}

FooListFragment

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()

您可以查看完整答案here

关于android - 在 submitList 之后没有得到 PagedListAdapter 的 itemCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57721097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com