Jetpack组合懒惰列交换不同的列表而不更新



我是喷气背包合成的新手,

我向Lazycolumn展示了一个运行良好的数据集。当我尝试切换,即用不同的数据集替换原始数据集时,我的Lazycolumn最初显示的是被替换的数据集,但很快它又回到了原始数据集

以下是我所做的一些片段,我怀疑我的写作逻辑,无法找到

//我观察变化并调用ShowList来填充的主要组合

@Composeable
fun SomeScreen(viewModel : TestviewModel){
val stateCountryCodeMap = remember { mutableStateOf(mapOf<String?, List<CountryInfo>>()) }
// observe and retrieve the dataset.
testViewModel.stateMapCountryInfo.collectAsState().value.let {
stateCountryCodeMap.value = it
}
// Some Test to buttom to load a different data set
someRandomeButtom.click{
viewModel. filterCountryList()
}
// request to load original data set
testViewModel.fetchCountryList()

ShowList(
currentSelected = stateCountryCodeSelectedIndex.value,
groupedCountryInfo = stateCountryCodeMap.value,
testViewModel = testViewModel
)
}

//显示的ShowList功能

@Composable
private fun ShowList(
currentSelected: Pair<String, Int>,
groupedCountryInfo: Map<String?, List<CountryInfo>>,
testViewModel: TestViewModel
) {
// LazyColumn stuff to render itmems from map dataset
}

//和TestviewModel

val stateMapCountryInfo = MutableStateFlow(mapOf<String?, List<CountryInfo>>())
val stateSortedCountryInfo = MutableStateFlow(listOf<CountryInfo>())
fun fetchCountryList() {
// some IO operation which gives result
when (val result = getCountryListRepo.invoke()) {
is Result.Success -> {
val countryInfoResultList = result.data
// sort the list by country name and store
stateSortedCountryInfo.value  = countryInfoResultList.sortedBy { it.countryName }
// save it to map
stateMapCountryInfo.value = stateSortedCountryInfo.value.groupBy { it.countryName?.get(Constants.ZERO).toString() }

}
}


val stateFilteredCountryInfo = MutableStateFlow(listOf<CountryInfo>())
fun filterCountryList() {
// some IO operation 
// filter on the sorted array, // results 2 items -  India, Indonesia
val filteredList = stateSortedCountryInfo.value.filter {
it.countryName?.contains("Ind") == true  
}

// store the filtered result
stateFilteredCountryInfo.value = filteredList


// now assing it to map
stateMapCountryInfo.value  = stateFilteredCountryInfo.value.groupBy { it.countryName?.get(Constants.ZERO).toString() }
}
}
}

到目前为止,它是ShowList方法中项目的直接显示。

现在,回到SomeScreenMethod(..(,

现在,如果我点击那个随机按钮,它会给我一个不同的/过滤的列表LazyColumn对其进行更新,但随后再次返回到原始状态。

你能指出哪里出了问题吗?

当重组发生时,似乎会被多次触发。

testViewModel.fetchCountryList((

你能试试这个并检查吗

LaunchedEffect(Unit) {
testViewModel.fetchCountryList()
}

最新更新