我创建了一个 LazyColumn,它从 viewModel 获取项目,并且一切都工作正常,但我想要的是,当将新项目插入到惰性列时,我希望新项目的背景颜色为绿色 2 秒然后它又变回白色。这就是我为实现这一目标所做的事情,但该项目一直保持绿色:
@Composable
fun SingleItem(item: Item) {
val new = remember {
mutableStateOf(true)
}
val color: MutableState<Color> = remember {
if (new.value)
mutableStateOf(Color(0xFFB9F6CA))
else
mutableStateOf(Color(0xFFFDFDFD))
}
Card(
modifier = Modifier
.padding(4.dp)
.fillMaxWidth(),
shape = RoundedCornerShape(8.dp),
backgroundColor = color.value
) {
GlobalScope.launch {
delay(2000)
new.value= !new.value
}
Column(
modifier = Modifier
.fillMaxWidth(),
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.SpaceBetween
) {
Text(text = item.name, style = MaterialTheme.typography.h5)
Text(text = "${item.quantity}", style = MaterialTheme.typography.h6)
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 Room 构建一个数据库,但我不知道如何将具有关系(在我的例子中是一对多)的新元素插入到数据库中。没有解决方案曾经讨论过插入(他们只讨论了查询数据)。
这是 DAO:
@Dao
abstract class ShoppingListsDao {
@Insert
abstract suspend fun addNewShoppingList(newShoppingList: ShoppingList)
@Insert
abstract suspend fun addNewItem(newItem: Item)
// This is how I thought it would work but it didn't
@Insert
@Transaction
abstract suspend fun addNewShoppingListWithItems(newShoppingListWithItems: ShoppingListWithItems)
}
Run Code Online (Sandbox Code Playgroud)
这是我的实体:
@Dao
abstract class ShoppingListsDao {
@Insert
abstract suspend fun addNewShoppingList(newShoppingList: ShoppingList)
@Insert
abstract suspend fun addNewItem(newItem: Item)
// This is how I thought it would work but it didn't
@Insert
@Transaction
abstract suspend fun addNewShoppingListWithItems(newShoppingListWithItems: ShoppingListWithItems)
}
Run Code Online (Sandbox Code Playgroud) 我构建了一个惰性列,它从 ViewModel 获取数据,每次添加一个项目时,惰性列都会重新组合,并且新项目会在惰性列中查看,但是当我添加删除项目的功能时,惰性列会执行此操作不会重新组合,并且删除的项目将保留在屏幕上,直到发生配置更改(例如旋转屏幕)。
我的问题是如何使惰性列知道每次删除操作后列表已更新。
这是视图模型:
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>?
}
}
Run Code Online (Sandbox Code Playgroud)
,显示惰性列的 Compsable:
content = { paddingValues ->
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.padding(8.dp)
) {
OutlinedTextField(
value = text,
onValueChange = { textFieldsViewModel.onShoppingListNameChanged(it) },
label …
Run Code Online (Sandbox Code Playgroud) 我的应用程序中有一个屏幕,允许用户编辑数据库中的项目,在用户完成编辑项目后,他们单击“完成”按钮,将他们带到上一个屏幕。但是当用户单击后退按钮时,他们将再次进入编辑屏幕。
我想要的是当用户单击后退按钮时转到另一个屏幕,就像他们从未去过编辑屏幕一样。
这是我的导航图:
@Composable
fun AppNavigator(
textFieldsViewModel: TextFieldsViewModel,
shoppingListsViewModel: ShoppingListsViewModel,
addNewShoppingListViewModel: AddNewShoppingListViewModel
) {
val navController = rememberNavController()
NavHost(navController, startDestination = "SplashScreen") {
composable("SplashScreen") {
SplashScreen(navController = navController)
}
composable("shoppingLists") {
ShoppingListsScreen(
navController = navController,
shoppingListsViewModel = shoppingListsViewModel,
textFieldsViewModel = textFieldsViewModel
)
}
composable(
"ItemsList/{listID}",
arguments = listOf(navArgument("listID") { type = NavType.IntType })
) {
ShoppingListItemsScreen(
it.arguments?.getInt("listID")!!,
shoppingListsViewModel = shoppingListsViewModel,
textFieldsViewModel = textFieldsViewModel,
navController = navController
)
}
composable(
"EditItem/{itemId},{itemName},{itemQuantity},{itemParentListId}",
arguments = listOf(
navArgument("itemId") { type = NavType.IntType },
navArgument("itemName") …
Run Code Online (Sandbox Code Playgroud) kotlin android-jetpack android-architecture-navigation android-jetpack-compose jetpack-compose-navigation