Skip to content

Commit 0fd02a1

Browse files
authored
Merge pull request #351 from android/snap-scroll-button
Adds the code snippet on https://developer.android.com/quick-guides/c…
2 parents 3d6012a + b1d46e8 commit 0fd02a1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/lists/LazyListSnippets.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
4747
import androidx.compose.foundation.lazy.staggeredgrid.items
4848
import androidx.compose.foundation.rememberScrollState
4949
import androidx.compose.foundation.verticalScroll
50+
import androidx.compose.material3.Button
5051
import androidx.compose.material3.Text
5152
import androidx.compose.runtime.Composable
5253
import androidx.compose.runtime.LaunchedEffect
@@ -742,3 +743,28 @@ private val randomSizedPhotos = listOf(
742743
randomSampleImageUrl(width = 1600, height = 900),
743744
randomSampleImageUrl(width = 500, height = 500),
744745
)
746+
747+
// [START android_compose_lists_snap_scroll_button]
748+
@Composable
749+
fun MessageList(modifier: Modifier = Modifier) {
750+
val listState = rememberLazyListState()
751+
val coroutineScope = rememberCoroutineScope()
752+
753+
LazyColumn(state = listState, modifier = Modifier.height(120.dp)) {
754+
items(10) { index ->
755+
Text(
756+
modifier = Modifier.height(40.dp),
757+
text = "Item $index"
758+
)
759+
}
760+
}
761+
762+
Button(onClick = {
763+
coroutineScope.launch {
764+
listState.animateScrollToItem(index = 0)
765+
}
766+
}) {
767+
Text(text = "Go top")
768+
}
769+
}
770+
// [END android_compose_lists_snap_scroll_button]

0 commit comments

Comments
 (0)