Compose Lazy列表如何避免不必要的重组

147 阅读1分钟

ViewModel中的ViewState中的List使用

val list: SnapshotStateList<Item> = mutableStateListOf<Item>

相较于普通List,SnapshotStateList不需要更新整个list才能触发State变化,只要对SnapshotStateList中的任意一个item进行修改就会触发State变化

使用remember定义点击事件

val onClick = remember {
    { index: Int ->
        vm.phoneAlbumDelegation.toggleChecked(index)
    }
}

MyItem(index, item, onClick)

如上,onClick使用remember定义,通过参数传递给MyItem,这样当点击某一个item时,只会触发点击的那一个item重组,不会触发其他item的重组