gpt4 book ai didi

android - 如何使用 Android androidx.paging.PagingDataAdapter loadStateFlow 来显示 Empty、Loading 和 Loaded UI 状态

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

我当前的 android 应用程序使用 Room Paging3 库

implementation 'androidx.paging:paging-runtime-ktx:3.0.0-beta01'
我的要求是显示“数据加载微调器”、没有数据可用时的空消息或加载的数据。
我可以在加载时成功显示加载微调器或数据,但是当没有数据显示时它不起作用。
我已经尝试过 SO 上提供的解决方案,但是没有一个能解决我的问题
成功处理加载和加载数据的代码版本类似于:-
viewLifecycleOwner.lifecycleScope.launch {
adapter.loadStateFlow
.distinctUntilChangedBy { it.refresh }
.filter { it.refresh is LoadState.NotLoading }
.flowOn(Dispatchers.Default)
.collectLatest {
withContext(Dispatchers.Main) {
if (adapter.itemCount == 0) (viewModel as HomeViewModel).loading.value = View.VISIBLE
else (viewModel as HomeViewModel).loading.value = View.GONE
}
}
}
我也尝试使用这个投票给 SO 答案来显示空状态,但是它对我不起作用
adapter.addLoadStateListener { loadState ->
if (loadState.source.refresh is LoadState.NotLoading && loadState.append.endOfPaginationReached && adapter.itemCount < 1) {
recycleView?.isVisible = false
emptyView?.isVisible = true
} else {
recycleView?.isVisible = true
emptyView?.isVisible = false
}
}
该问题是由发出此条件的 loadStateFlow 引起的
(loadState.source.refresh is LoadState.NotLoading && loadState.append.endOfPaginationReached && adapter.itemCount < 1)
在加载数据时。
我的列表屏幕已拉动刷新,在数据刷新过程中,我首先删除所有现有行,然后远程获取所有数据。
无论如何我可以使用 PagingDataAdapter.loadStateFlow显示我的三种状态 Loading、Loaded 或 Empty?
我的 ViewModel代码类似于:-
repository.myData()
.map { pagingData ->
pagingData.map { myDO -> myDO.mapUI() }
}
.cachedIn(viewModelScope)
.collect {
emit(it)
}
我的 Repository代码类似于:-
fun myData(): Flow<PagingData<MyDO>> {
return Pager(
PagingConfig(pageSize = 60, prefetchDistance = 30, enablePlaceholders = false, maxSize = 200)
) {
database.myDAO().fetchMyData()
}.flow.flowOn(Dispatchers.IO)
}
我的房间查询类似于这个
@Query("SELECT * from my_table ORDER BY position_date DESC")
fun fetchMyData(): PagingSource<Int, MyDO>
我哪里出错了?
是否有可能实现我想要的功能?
更新
我相信我有两个不同的问题。
第一个是由我的初始数据同步引起的,我使用 Android Workers 从远程源下载数据并插入到我的本地 Room 数据库中。
由于远程数据的组织方式和可用的 Restful API,列表中显示的数据可以由多个工作人员插入。本地插入的数据的这种“分区”导致提交给我的 Paging3 适配器的数据是零星的,这意味着加载状态循环通过加载 -> 未加载 -> 加载 -> 未加载。
我在同步进行时显示列表 fragment ,这导致加载 Spinner 和显示的数据列表之间的屏幕内容“闪烁”

最佳答案

我开发了一种方法来检索 加载中 , , 和 错误 来自 CombinedLoadStates 实例的状态.你可以像下面这样使用它:

adapter.addLoadStateListener { loadState ->
loadState.decideOnState(
showLoading = { visible ->
(viewModel as HomeViewModel).loading.value =
if (visible) View.VISIBLE else View.GONE
},
showEmptyState = { visible ->
emptyView?.isVisible = visible
recycleView?.isVisible = !visible
},
showError = { message ->
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}
)
}
private inline fun CombinedLoadStates.decideOnState(
showLoading: (Boolean) -> Unit,
showEmptyState: (Boolean) -> Unit,
showError: (String) -> Unit
) {
showLoading(refresh is LoadState.Loading)

showEmptyState(
source.append is LoadState.NotLoading
&& source.append.endOfPaginationReached
&& adapter.itemCount == 0
)

val errorState = source.append as? LoadState.Error
?: source.prepend as? LoadState.Error
?: source.refresh as? LoadState.Error
?: append as? LoadState.Error
?: prepend as? LoadState.Error
?: refresh as? LoadState.Error

errorState?.let { showError(it.error.toString()) }
}

关于android - 如何使用 Android androidx.paging.PagingDataAdapter loadStateFlow 来显示 Empty、Loading 和 Loaded UI 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66471381/

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