- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Jetpack Compose 的新手,我花了几个小时来寻找如何让 LazyColumn 更新我更新我的列表。我读过它需要是一个不可变的列表来更新 LazyColumn,但我似乎无法让它工作。
代码如下:
@Composable
fun CreateList() {
var myList : List<DailyItem> by remember { mutableStateOf(listOf())}
myList = getDailyItemList() // Returns a List<DailyItem> with latest values and uses mutable list internally
// Function to refresh the list
val onUpdateClick = {
// Do something that updates the list
...
// Get the updated list to trigger a recompose
myList = getDailyItemList()
}
// Create the lazy column
...
}
我已经尝试了几件事,要么在点击更新按钮时列表从未更新,要么只更新第一个项目而不更新列表中的其余项目。我查看了文档,上面写着,但我不明白:
Instead of using non-observable mutable objects, we recommend you usean observable data holder such as State<List> and the immutablelistOf().
最佳答案
使用 SnapshotStateList
,列表是可变的。对列表的任何修改(添加、删除、清除...)都将触发 LazyColumn
中的更新。 .
类似于 mutableListOf()
(对于 MutableList
)有 mutableStateListOf()
创建一个 SnapshotStateList
.
扩展功能swapList()
只是结合clear()
和 addAll()
调用用新列表替换旧列表。
fun <T> SnapshotStateList<T>.swapList(newList: List<T>){
clear()
addAll(newList)
}
@Composable
fun CreateList() {
val myList = remember { mutableStateListOf<DailyItem>() }
myList.swapList(getDailyItemList()) // Returns a List<DailyItem> with latest values and uses mutable list internally
// Function to refresh the list
val onUpdateClick = {
// Do something that updates the list
...
// Get the updated list to trigger a recompose
myList.swapList(getDailyItemList())
}
// Create the lazy column
...
}
关于android-jetpack-compose - LazyColumn 和可变列表 - 如何更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68046535/
我收到此错误消息,但我不明白。 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:列出一些项目
我是一名优秀的程序员,十分优秀!