我构建了一个从 ViewModel 获取数据的惰性列,每次添加项时,惰性列都会重新组合,并且正在惰性列中查看新项,但是当我添加删除功能时一个 Item 懒惰的列不会重新组合,移除的项目会保留在屏幕上,直到发生配置更改(例如旋转屏幕)。
我的问题是如何让惰性列知道在每次删除操作后列表已经更新。
这是视图模型:
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.shoppinglistcompose.entities.Item
class AddNewShoppingListViewModel : ViewModel() {
val items = MutableLiveData<MutableList<Item>>(mutableListOf())
fun addNewItem(newItem: Item) {
items.value = items.value?.plus(listOf(newItem)) as MutableList<Item>?
}
fun removeItem(item: Item) {
items.value!!.remove(item)
items.value = items.value?.plus(listOf()) as MutableList<Item>?
}
}
,显示 Lazy Column 的 Compsable:
content = { paddingValues ->
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.padding(8.dp)
) {
OutlinedTextField(
value = text,
onValueChange = { textFieldsViewModel.onShoppingListNameChanged(it) },
label = { Text(text = "Shopping list Name") },
modifier = Modifier
.fillMaxWidth()
.padding(start = 30.dp, end = 30.dp, bottom = 8.dp)
)
if (!items.value?.isEmpty()!!) {
LazyColumn(
Modifier
.animateContentSize()
.fillMaxSize()
.padding(paddingValues)
) {
items(items = items.value!!) {
SingleItem(item = it) {
addNewShoppingListViewModel.removeItem(it)
}
}
}
} else {
Box(
modifier = Modifier
.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(
text = "There are no shopping Items",
color = Color.Red,
style = MaterialTheme.typography.h5
)
}
}
}
if (showDialog.value) {
MyDialog(
showDialog = showDialog,
textFieldsViewModel = textFieldsViewModel,
) {
val name = textFieldsViewModel.shoppingItemText.value
val quantity = textFieldsViewModel.itemQuantityText.value
val item = Item(0, name!!, quantity!!.toInt(), 0)
addNewShoppingListViewModel.addNewItem(item)
textFieldsViewModel.shoppingItemText.value = ""
textFieldsViewModel.itemQuantityText.value = ""
showDialog.value = !showDialog.value
}
}
},
这是 Item 它自身的可组合乐趣:
@Composable
fun SingleItem(item: Item, deleteClick: () -> Unit) {
val composableScope = rememberCoroutineScope()
val new: MutableState<Boolean> = remember {
mutableStateOf(true)
}
val color = remember { Animatable(Color(0xFFB9F6CA)) }
LaunchedEffect(new.value) {
color.animateTo(if (new.value) Color(0xFFB9F6CA) else Color(0xFFE0F2F1))
}
Card(
modifier = Modifier
.padding(4.dp)
.fillMaxWidth(),
shape = RoundedCornerShape(8.dp),
backgroundColor = color.value,
elevation = 3.dp
) {
composableScope.launch {
delay(2000)
new.value = false
}
Row(modifier = Modifier.fillMaxWidth() , horizontalArrangement = Arrangement.SpaceBetween) {
Column(
modifier = Modifier,
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.SpaceBetween
) {
Text(text = item.name, style = MaterialTheme.typography.h5)
Text(text = "${item.quantity}", style = MaterialTheme.typography.h6)
}
IconButton(onClick = deleteClick) {
Icon(Icons.Rounded.Delete, "Button for deleting an Item of the items list.")
}
}
}
}
答案 0 :(得分:2)
尝试替换该行,
items.value!!.remove(item)
items.value = items.value?.plus(listOf()) as MutableList<Item>?
与,
items.value = items.value?.filter { it != item }?.toMutableList()
问题是表达式 items.value?.plus(listOf())
不会产生不同的实例,并且 ViewModel
不会重新发布该值,因为它是 ==
到先前的值。这是因为 Kotlin 只会在列表表达式的操作符之一不等于结果时才分配一个新列表。
使用上面的 filter
可确保如果 item
在列表中,则生成一个新列表。