Skip to content

Instantly share code, notes, and snippets.

@heatcreep
Created December 9, 2023 00:17
Show Gist options
  • Save heatcreep/08ec25591b53b517ffb664856e4b8f10 to your computer and use it in GitHub Desktop.
Save heatcreep/08ec25591b53b517ffb664856e4b8f10 to your computer and use it in GitHub Desktop.
MonolithCollapsableColumn
@Composable
fun MonolithCollapsableColumn(listState: LazyGridState, content: @Composable () -> Unit) {
val contentShown = remember { mutableStateOf(true) }
var previousIndex by remember { mutableIntStateOf(listState.firstVisibleItemIndex) }
var previousScrollOffset by remember { mutableIntStateOf(listState.firstVisibleItemScrollOffset) }
LaunchedEffect(listState) {
snapshotFlow { listState.firstVisibleItemIndex }
.map { index ->
if (previousIndex != index) {
previousIndex > index
} else {
previousScrollOffset >= listState.firstVisibleItemScrollOffset
}.also {
previousIndex = index
previousScrollOffset = listState.firstVisibleItemScrollOffset
}
}
.collect { isScrollingUp ->
contentShown.value = isScrollingUp
}
}
Column {
AnimatedVisibility(visible = contentShown.value) {
Column(
modifier = Modifier
.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
content()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment