Skip to content

Commit 125b5bc

Browse files
committed
Adds the code snippet on https://developer.android.com/quick-guides/content/enable-snap-scrolling?hl=en (Create a button to enable snap scrolling)to the repo.
1 parent 351fcb4 commit 125b5bc

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

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

Lines changed: 27 additions & 1 deletion
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
@@ -67,11 +68,11 @@ import androidx.paging.compose.itemKey
6768
import coil.compose.AsyncImage
6869
import coil.compose.rememberAsyncImagePainter
6970
import com.example.compose.snippets.util.randomSampleImageUrl
70-
import kotlin.random.Random
7171
import kotlinx.coroutines.flow.distinctUntilChanged
7272
import kotlinx.coroutines.flow.filter
7373
import kotlinx.coroutines.flow.map
7474
import kotlinx.coroutines.launch
75+
import kotlin.random.Random
7576

7677
private object ListsSnippetsColumn {
7778
// [START android_compose_layouts_list_column]
@@ -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)