rememberScrollState
Composable Function
Common
@Composable
fun rememberScrollState(initial: Int = 0): ScrollState
Create and remember
the ScrollState
based on the currently appropriate scroll configuration
to allow changing scroll position or observing scroll behavior.
Learn how to control the state of Modifier.verticalScroll
or Modifier.horizontalScroll
:
Parameters
initial | initial scroller position to start with |
Code Examples
ControlledScrollableRowSample
@Composable
fun ControlledScrollableRowSample() {
// Create ScrollState to own it and be able to control scroll behaviour of scrollable Row below
val scrollState = rememberScrollState()
val scope = rememberCoroutineScope()
Column {
Row(Modifier.horizontalScroll(scrollState)) { repeat(1000) { index -> Square(index) } }
// Controls for scrolling
Row(verticalAlignment = Alignment.CenterVertically) {
Text("Scroll")
Button(onClick = { scope.launch { scrollState.scrollTo(scrollState.value - 1000) } }) {
Text("< -")
}
Button(onClick = { scope.launch { scrollState.scrollBy(10000f) } }) { Text("--- >") }
}
Row(verticalAlignment = Alignment.CenterVertically) {
Text("Smooth Scroll")
Button(
onClick = { scope.launch { scrollState.animateScrollTo(scrollState.value - 1000) } }
) {
Text("< -")
}
Button(onClick = { scope.launch { scrollState.animateScrollBy(10000f) } }) {
Text("--- >")
}
}
}
}