简体   繁体   English

当列表项属性发生变化时,Livedata 不更新 compose state

[英]Livedata don't update compose state when list item property has changed

My problem is that live data observer is triggered Observer<T> { state.value = it } with the correct data but compose doesn't kick on recompose.我的问题是实时数据观察器被触发Observer<T> { state.value = it }使用正确的数据但撰写不会启动重组。 Only when I add an item all changes are propagated.只有当我添加一个项目时,所有更改才会传播。 There must some checking on the list itself if it has changed.如果列表已更改,则必须对列表本身进行一些检查。 I guess it doens't compare list items.我想它不会比较列表项。

@Composable
fun <R, T : R> LiveData<T>.observeAsState(initial: R): State<R> {
    val lifecycleOwner = LifecycleOwnerAmbient.current
    val state = remember { mutableStateOf(initial) }
    onCommit(this, lifecycleOwner) {
        val observer = Observer<T> { state.value = it }
        observe(lifecycleOwner, observer)
        onDispose { removeObserver(observer) }
    }
    return state
}

val items: List<TrackedActivityWithMetric> by vm.activities.observeAsState(mutableListOf())
 LazyColumnForIndexed(
        items = items,
        Modifier.padding(8.dp)
    ) { index, item ->
    ....
    MetricBlock(item.past[1], item.activity.id )

}

So behind the scenes there must be some kind hash comparing mechanism preventing rendering same item twice (More elabored answer wanted).所以在幕后必须有某种 hash 比较机制防止渲染相同的项目两次(需要更详细的答案)。 The incorrect rendering was caused by property which was not in TrackedActivityWithMetric data class constructor.不正确的呈现是由不在TrackedActivityWithMetric数据 class 构造函数中的属性引起的。

Jetpack Compose does not work well with MutableList, you need to use a List and do something like this: Jetpack Compose 不能很好地与 MutableList 配合使用,您需要使用 List 并执行如下操作:

var myList: List<MyItem> by mutableStateOf(listOf())
    private set

for adding an item:添加项目:

fun addItem(item: MyItem) {
    myList = myList + listOf(myItem)
}

for editing an item:用于编辑项目:

fun editItem(item: MyItem) {
    val index = myList.indexOf(myItem)
    myList = myList.toMutableList().also {
        it[index] = myItem
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当列表项的DetailsActivity中的属性发生更改时,如何维护列表项的状态? - Maintaining List item state when List item property changed in its DetailsActivity? 更新数据库时,LiveData List不会更新 - LiveData List doesn't update when updating database 当一项状态发生变化时刷新 LazyPagingItems - Refreshing LazyPagingItems when one item state has changed 回收站视图不会使用实时数据更新项目 - Recycler view doesn't update item with livedata State 不更新 - State don't update 当项目添加到 LiveData 列表时通知观察者 - Notify Observer when item is added to List of LiveData Jetpack compose mutableStateOf 列表在更改列表项中的属性值时不会触发重新组合 class - Jetpack compose mutableStateOf list doesn't trigger re-composition when changing property value in list item class 属性更改后,数据绑定活动不会更新值 - Databinding activity does not update value when property has changed 为什么将新项目添加到我的 livedata 列表时我的观察者没有收到通知 - Why does my observer doesn't get notified when a new item is added to my livedata list 我不明白为什么当我将列表更改为 mutableStateOf 时 Jetpack Compose 不重新组合? - I don't understand why Jetpack Compose don't recompose when I change list into a mutableStateOf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM