verticalScroll / horizontalScroll · Horizontal
A scrolling row.
Open the live, interactive demo →
Change its controls, read the remote document it produces and the diff after every change.
What it is
Lets content that is longer or wider than its box scroll, with optional snapping, handled entirely on the device.
Good for
- Lists and feeds
- Horizontal carousels
Code
The story's source with androidx.compose.remote imports. CI compiles every snippet on this site against AndroidX.
import androidx.compose.remote.creation.compose.layout.RemoteAlignment
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteRow
import androidx.compose.remote.creation.compose.layout.RemoteText
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.horizontalScroll
import androidx.compose.remote.creation.compose.modifier.padding
import androidx.compose.remote.creation.compose.modifier.rememberRemoteScrollState
import androidx.compose.remote.creation.compose.modifier.size
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
@Composable
@RemoteComposable
fun HorizontalScroll(itemCount: Int) {
val scrollState = rememberRemoteScrollState()
RemoteRow(
RemoteModifier.size(300.rdp, 100.rdp)
.background(Color(0xFFF1F3F4).rc)
.horizontalScroll(scrollState),
verticalAlignment = RemoteAlignment.CenterVertically,
) {
for (i in 1..itemCount) {
RemoteBox(
RemoteModifier.size(80.rdp).padding(4.rdp).background(Color(0xFFEF5350).rc),
contentAlignment = RemoteAlignment.Center,
) {
RemoteText("$i".rs, color = Color.White.rc)
}
}
}
}
API
creation.compose.modifier.verticalScroll
RemoteModifier verticalScroll(RemoteModifier, RemoteScrollState state)creation.compose.modifier.horizontalScroll
RemoteModifier horizontalScroll(RemoteModifier, RemoteScrollState state)creation.compose.modifier.rememberRemoteScrollState
RemoteScrollState rememberRemoteScrollState(optional int notches)creation.compose.modifier.RemoteScrollState
class RemoteScrollState
RemoteScrollState(MutableRemoteFloat positionState, int notches)
RemoteScrollState(float position, int notches)
RemoteScrollState(optional int notches)
int notches
MutableRemoteFloat positionState