gpt4 book ai didi

Android 回收站 View 最初总是空的

转载 作者:行者123 更新时间:2023-11-29 18:26:29 28 4
gpt4 key购买 nike

我有一个像这样的 View 模型 -

private val viewState = SchoolsViewState()

fun onViewOpened() =
Transformations.map(schoolUseCase.performAction(SchoolAction.GetList)) {
when (it) {
is SchoolResult.Loading -> viewState.copy(loading = true)
is SchoolResult.ListContent -> viewState.copy(loading = false, listData = it.schoolList)
is SchoolResult.Error -> viewState.copy(loading = false, error = it.error)
else -> viewState.copy(loading = false)
}
}

viewState 类看起来像这样 -

data class SchoolsViewState(
val loading: Boolean = false,
val schoolList: List<SchoolModel> = emptyList(),
val error: SchoolResult.ErrorType? = null
)

Fragment View 模型观察代码如下所示 -

viewModel.onViewOpened().observe(this, Observer {
//Handling the SchoolsViewState here
swipeContainer.isRefreshing = it.loading

if (it.schoolList.isNullOrEmpty()) {
view?.recycler_view?.visibility = View.GONE
view?.empty_view?.visibility = View.VISIBLE
} else {
view?.recycler_view?.visibility = View.VISIBLE
view?.empty_view?.visibility = View.GONE
view?.recycler_view?.adapter = schoolAdapter
myAdapter.setSchoolList(it.schoolList)
}
})

问题出在每次滑动刷新时,我首先看到的是空 View ,一旦有数据,我就会看到学校列表。所以这是一种 UI 不便。这是由于 viewState.copy(...) 在每次刷新时都会为 UI 提供新状态。我该如何克服这个问题?

最佳答案

如果列表为空,则不应显示空 View ,因为列表仍在加载中 - 这没有意义。相反,您应该仅在 loading = false 并且您的列表为空时显示它。在这种情况下,您真的别无选择。因此,您的代码可能如下所示:

viewModel.onViewOpened().observe(this, Observer {
//Handling the SchoolsViewState here
swipeContainer.isRefreshing = it.loading

// if means the loading is finished
if (!it.loading) {
if (it.schoolList.isNullOrEmpty()) {
view?.recycler_view?.visibility = View.GONE
view?.empty_view?.visibility = View.VISIBLE
} else {
view?.recycler_view?.visibility = View.VISIBLE
view?.empty_view?.visibility = View.GONE
view?.recycler_view?.adapter = schoolAdapter
myAdapter.setSchoolList(it.schoolList)
}
} else {
// here you could show some loading progress or similar
}
})

关于Android 回收站 View 最初总是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58680097/

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