Class

ScrollState

State of the scroll.

ControlledScrollableRowSample

@Sampled
@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("--- >")
            }
        }
    }
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: