- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Jetpack Compose
在单击按钮时删除文本,但 UI 未更新。
我尝试设置一个按钮,然后单击一个按钮,deletedTasks 得到更新,如果我的任务在 deletedTasks 中,那么它应该显示一个删除线.
此更改仅手动发生,但我希望它是自动的
我不知道该怎么做。这是我第一次尝试 Jetpack Compose
,我们将不胜感激
@Composable
fun toDoItem(title: String, category: String, task: Task) {
val rememberTask = remember { mutableStateOf(deletedTasks) }
Surface(...) {
Column (...) {
Row {
Column (...) {
if (!deletedTasks.contains(task)){
Text(text = title, style = MaterialTheme.typography.h4.copy(
fontWeight = FontWeight.Bold
))
} else {
Text(text = title, style = MaterialTheme.typography.h4.copy(
fontWeight = FontWeight.Bold
), textDecoration = TextDecoration.LineThrough)
}
}
IconButton(onClick = {
rememberTask.value.add(task)
}) {
Icon(imageVector = Icons.Filled.Check, contentDescription = "Check")
}
}
}
}
}
@Composable
fun recyclerView(tasks: MutableList<Task>) {
LazyColumn (modifier = Modifier.padding(vertical = 10.dp, horizontal = 80.dp)) {
items(items = tasks) {task ->
toDoItem(task.title, task.category, task)
}
}
}
最佳答案
您正在使用 MutableList<>
,不建议将其用于 Jetpack Compose
中的任何集合用例.我建议将其更改为 SnapshotStateList
.
您的 recyclerView 可组合项看起来像这样
@Composable
fun recyclerView(tasks: SnapshotStateList<Task>) { ... }
在你设置东西的地方(例如 ViewModel
)就像
tasks = mutableStateListOf<Task>( ... )
使用 SnapshotStateList
将保证“更新”或re-composition
使用您对它执行的任何正常列表操作,例如,
list.add( <new task> )
list.remove( <selected task> )
list[index] = task.copy()
<- 更新列表的惯用方式还有SnapshotStateMap
这是Map key~value
对用例。
但是如果您对如何更新 LazyColumn
感到好奇用普通的List
,您将不得不重新创建整个 MutableList<Task>
这样它将通知 Composer 进行了更新,因为 MutableList<Task>
现在指的是任务集合的新引用。
关于android - Jetpack 撰写 : LazyColumn not updating with MutableList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74134219/
我收到此错误消息,但我不明白。 java.lang.IllegalState Exception: Vertically scrollable component was measured with
我已经完成了 this代码实验室。在第 7 步中,当单击单行文本时,它会改变颜色,但函数不会跟踪它,这意味着它会在重新组合后消失。 我希望列表记住单个项目的颜色,因此我已将状态提升移至 NameLis
谁能解释一下 的主要目的是什么? ' key ' LazyListScope 的 items/itemsIndexed 函数内的参数?如果我们指定该参数,我们会得到什么或没有得到什么?我不确定我是否了
@Composable fun ShowScreen(viewModel: UseListViewModel){ Column() { ShowList(viewModel
我有一个 LazyColumn,我想在其中显示一个包含两列的水平行,因此我尝试使用 LazyHorizontalGrid 来实现它。但是我的应用程序因异常而崩溃 - IllegalArgument
我正在创建一个应用程序,除其他外,它使用户能够添加消费者,然后再将其删除。消费者显示在最后带有删除按钮的卡片中。 添加消费者工作正常。但是,当我尝试删除消费者时,应用屏幕中删除的始终是最后一个。我知道
我有一个带有项目的惰性列,我想在每次其中一个项目出现在屏幕上时发送一个事件。有第一次发送事件的示例(如此处 https://plusmobileapps.com/2022/05/04/lazy-col
我有下一个代码,一切都很好,只是我需要将第二个 AnswerOption 放在 lazyverticalGrid 中。我做不到,因为它在一个惰性列中,这是代码 @Composable fun Asse
@Composable fun PreviewLayout() { fun getRandomString(length: Int): String { val allowed
如何从下面显示惰性列?默认情况下,它会显示上面的列表 LazyColumn(){ itemsIndexed(items = chat){ index, chat ->
我需要检查某个 LazyColumn item 出现,一旦出现,就回调 onItemWithKeyViewed()仅一次通知该项目已被查看。 我的尝试: @Composable fun Special
我正在尝试实现一个底部带有按钮的列表,如下所示: 因为我不知道列表中有多少项目,所以我使用 LazyColumn .底部的按钮,只有当列表没有填满整个屏幕时才应该放在那里,如果是,按钮应该向下移动并成
我想选择我的 LazyColumn 中的一项并更改文本颜色。 如何识别选择了哪个项目? 代码: val items = listOf(Pair("A", 1), Pair("AA", 144), Pa
关注 Pathway谷歌关于 Jetpack compose 的代码实验室,我正在尝试这段代码 import android.os.Bundle import androidx.activity.co
我想创建一个 LazyColumn可以通过拖放重新排序的项目。如果没有 compose,我的方法是使用 ItemTouchHelper.SimpleCallback ,但我还没有找到类似的作曲。 我试
我希望得到这样的结果。两个分别为 2/3 大小和 1/3 大小的 block 。 我用这段代码得到了预期的结果。 @Preview(showBackground = true) @Composable
我想在下面的代码中将 LazyColumn 中的项目限制为 3, @Composable fun MessageList(messages: List) { LazyColumn {
我想在下面的代码中将 LazyColumn 中的项目限制为 3, @Composable fun MessageList(messages: List) { LazyColumn {
我在 BottomSheetDialogFragment 中使用了 LazyColumn,但是如果滚动 LazyColumn list UP 然后 Bottom Sheet Dialog 滚动Lazy
我正在尝试创建一个底部有隐藏按钮的项目列表。默认情况下,该按钮在屏幕上不可见。 如果用户滚动到列表底部,按钮应该显示为列表的最后一项。 我创建了插图以更好地形象化所需的行为: 草图 1:列出一些项目
我是一名优秀的程序员,十分优秀!