@@ -47,6 +47,7 @@ import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
47
47
import androidx.compose.foundation.lazy.staggeredgrid.items
48
48
import androidx.compose.foundation.rememberScrollState
49
49
import androidx.compose.foundation.verticalScroll
50
+ import androidx.compose.material3.Button
50
51
import androidx.compose.material3.Text
51
52
import androidx.compose.runtime.Composable
52
53
import androidx.compose.runtime.LaunchedEffect
@@ -67,11 +68,11 @@ import androidx.paging.compose.itemKey
67
68
import coil.compose.AsyncImage
68
69
import coil.compose.rememberAsyncImagePainter
69
70
import com.example.compose.snippets.util.randomSampleImageUrl
70
- import kotlin.random.Random
71
71
import kotlinx.coroutines.flow.distinctUntilChanged
72
72
import kotlinx.coroutines.flow.filter
73
73
import kotlinx.coroutines.flow.map
74
74
import kotlinx.coroutines.launch
75
+ import kotlin.random.Random
75
76
76
77
private object ListsSnippetsColumn {
77
78
// [START android_compose_layouts_list_column]
@@ -742,3 +743,28 @@ private val randomSizedPhotos = listOf(
742
743
randomSampleImageUrl(width = 1600 , height = 900 ),
743
744
randomSampleImageUrl(width = 500 , height = 500 ),
744
745
)
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