@Composable
fun MyList() {
ColumnList()
}
@Composable
fun ColumnList() {
Column {
repeat(500) {
Text("List Item $it",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(5.dp)
)
}
}
}
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun previewList() {
MyApplicationTheme {
MyList()
}
}

@Composable
fun ColumnList() {
val scrollState = rememberScrollState()
Column(Modifier.verticalScroll(scrollState)) {
repeat(500) {
Text("List Item $it",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(5.dp)
)
}
}
}
이번에 구현할 것은 버튼 클릭으로 리스트의 처음과 마지막으로 이동하는 것이다. 이 기능은 코틀린 함수이므로 코루틴 스코프 내에서 스크롤 동작을 초기화해야 한다.
@Composable
fun ColumnList() {
val scrollState = rememberScrollState()
val coroutineScope = rememberCoroutineScope()
Column {
Row() {
Button(onClick = {
coroutineScope.launch {
scrollState.animateScrollTo(0)
}
},
modifier = Modifier.weight(0.5f)
.padding(2.dp)) {
Text("Top")
}
Button(onClick = {
coroutineScope.launch {
scrollState.animateScrollTo(scrollState.maxValue)
}
}, modifier = Modifier.weight(0.5f)
.padding(2.dp)) {
Text("Bottom")
}
}
Column(Modifier.verticalScroll(scrollState)) {
repeat(500) {
Text(
"List Item $it",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(5.dp)
)
}
}
}
}
@Composable
fun RowList() {
val scrollState = rememberScrollState()
Row(Modifier.horizontalScroll(scrollState)) {
repeat(50) {
Text("List Item $it",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(5.dp))
}
}
}