Each time the TopAppBar collapses and expands, all of the LazyColumn elements are recomposed multiple times.
@OptIn(ExperimentalMaterial3Api::class, ExperimentalAnimationApi::class)
@Composable
fun RoomsScreen(
viewModel: ScreenViewModel = hiltViewModel()
) {
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())
scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
TopAppBar(
scrollBehavior = scrollBehavior
...
)
},
) { paddingModifier ->
LazyColumn(
modifier = Modifier
.padding(paddingModifier)
.fillMaxSize()
,
) {
items(viewModel.list.size) { index ->
RoomCard(
viewModel = viewModel.list[index],
)
}
}
}
}
It is clear that when collapsing and expanding TopAppBar, the paddingModifier changes, and the LazyColumn must be rebuilt, but it is not entirely clear why all nested elements should be rebuilt (hundreds of times while the movement takes place), this greatly squanders performance. What would you advise in this situation?
P.S: If you use pinnedScrollBehavior, of course, the problem disappears, but this is not what you want to get