可组合的 object.observeAsState() 更改后不清除以前的状态



我正在尝试在获取另一个数据时实现一个简单的加载CircularProgressIndicator()。当数据准备好后,设置正确的撰写内容。

代码一开始运行良好,加载指示器显示,然后数据准备就绪,Composable将数据设置在OtherComposeContent()上。但是加载指示符CircularProgressIndicator()并没有消失,而是停留在具有新数据的同一UI上。

我尝试了不同的实现方式,但没有成功。

val objectReady by objectViewModel.objectReady.observeAsState()
LaunchView(state = objectReady)
@Composable
fun LaunchView(state:Boolean?){
if (state == true){
OtherComposeContent() // New view when data is ready. Works fine
} else {
CircularProgressIndicator() // Loading circle while feetching data - Should dissapear when state is true but no
}
@AndroidEntryPoint
class EntryPointActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val photosVM: PhotosViewModel by viewModels()
setContent {
RbotTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
if (photosVM.snapshotStateList.isEmpty()) {
LoadingState()
} else
MainContent(photosVM.snapshotStateList)
}
}
}
}
}
@Composable
fun LoadingState(){
Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
CircularProgressIndicator()
Text(text = "Loading...", modifier = Modifier.padding(8.dp))
}
}
@Composable
fun MainContent(photoList: SnapshotStateList<PhotosModel>) {
Column {
LazyColumn(
Modifier.weight(1f),
contentPadding = PaddingValues(horizontal = 8.dp, vertical = 8.dp),
) {
items(
items = photoList,
itemContent = {
BasicCard(it)
})
}
}
}

完整样本代码

最新更新