我正在使用 Paging 3 lib。我可以检查刷新状态是“加载”还是“错误”,但我不确定如何检查“空”状态。我可以添加以下条件,但我不确定它的条件是否正确
adapter.loadStateFlow.collectLatest { loadStates ->
viewBinding.sflLoadingView.setVisibility(loadStates.refresh is LoadState.Loading)
viewBinding.llErrorView.setVisibility(loadStates.refresh is LoadState.Error)
viewBinding.button.setOnClickListener { pagingAdapter.refresh() }
if(loadStates.refresh is LoadState.NotLoading && (viewBinding.recyclerView.adapter as ConcatAdapter).itemCount == 0){
viewBinding.llEmptyView.setVisibility(true)
}else{
viewBinding.llEmptyView.setVisibility(false)
}
}
Run Code Online (Sandbox Code Playgroud)
另外我遇到了其他问题我已经实现了搜索功能,直到输入超过 2 个字符之前,我使用相同的分页源,如下所示,但上面的加载状态回调只执行一次。所以这就是为什么我无法隐藏空视图,如果搜索查询被清除。我这样做是为了保存前端的 api 调用。
private val originalList : LiveData<PagingData<ModelResponse>> = Transformations.switchMap(liveData){
repository.fetchSearchResults("").cachedIn(viewModelScope)
}
val list : LiveData<LiveData<PagingData<ModelResponse>>> = Transformations.switchMap{ query ->
if(query != null) {
if (query.length >= 2)
repository.fetchSearchResults(query)
else
originalList
}else
liveData { emptyList<ModelResponse>() }
}
Run Code Online (Sandbox Code Playgroud) android ×1