Ricardo C.
10/24/2023, 4:44 PMderivedStateOf
🧵Ricardo C.
10/24/2023, 4:44 PMval groupLazyListState = rememberLazyListState()
val topBarBackgroundAlpha by remember(groupLazyListState) {
derivedStateOf {
val layoutInfo = groupLazyListState.layoutInfo
val itemInfo = layoutInfo.visibleItemsInfo
.firstOrNull { it.contentType == ImmersiveCollectionUiModel::class }
?: return@derivedStateOf 1f
val adjustedOffset = itemInfo.offset.absoluteValue - layoutInfo.viewportStartOffset
adjustedOffset / itemInfo.size.toFloat()
}
}
I have some top menu items where I completely switch the following Group
composable. This ends up being totally new LazyColumns
underneath, but using the same LazyListState
.
Group(
model = group,
lazyListState = groupLazyListState,
)
When I’m switching between these menu items, I’ll end up in a state where derivedStateOf
stops being called and my topBarBackgroundAlpha
no longer changes. Group
is totally fine, scroll is working fine, its just that observing those scroll state changes will no longer work.
What am I missing here?Ricardo C.
10/24/2023, 5:10 PMrememberUpdatedState
seems to solve it, but its not very obvious right away why derivedStateOf
doesn’t retrigger on updated layoutInfo
.