MSpeed
MSpeed

Reputation: 8280

Jetpack Compose - LazyColumn children not redrawn on recompose

I have a very strange problem with my LazyColumn. I am updating the menuList State in the ViewModel, the view recomposes but the list doesn't get redrawn.

When I use the debugger, it gets to LazyColumn and then stops, which means the children aren’t redrawn with the new data. Any ideas why? Thanks!

MyView:

val menuList = viewModel.menuData.observeAsState()

LazyColumn() { // <- Debugger stops here
                items(menuList.value.sections.size) { i->
                    MenuItem(menuList.value[i])
                }
}

MyViewModel:

private var menu: MenuUiState? = null
 val menuData: MutableLiveData<MenuUiState> by lazy {
    MutableLiveData<MenuUiState>()
}

// ...
menu?.sections?.forEach {
    //update menu properties here
}
menuData.value = menu?.copy()

Upvotes: 1

Views: 923

Answers (1)

jpegoraro
jpegoraro

Reputation: 504

You are observing the MenuUiState object, but you are not actually observing changes made to the items of the sections list.

You should either make sections a MutableStateList inside MenuUiState, or have the ViewModel store it directly:

val sections = mutableStateListOf<Section>() // store it either inside the ViewModel or inside MenuUiState

Then just observe it in the composable normally:

LazyColumn() { // <- Debugger stops here
      items(viewModel.sections) { section ->
          MenuItem(section)
      }
}

Upvotes: 2

Related Questions