Compose Column 实现滚动

 // 我们用这个状态保存滚动位置
    val scrollState = rememberScrollState()
    Column(Modifier.verticalScroll(scrollState)) {
         。。。
    }
//滚动列表控件
 LazyColumn {
        items(100) { //注意这里不是repeat,
            Text("Item #$it", style = MaterialTheme.typography.subtitle1)
        }
    }
    val listSize = 100
    // 保存滚动状态
    val scrollState = rememberLazyListState()
    // 保存将执行动画滚动的协程范围
    val coroutineScope = rememberCoroutineScope()
    Column {
        Row {
            Button(onClick = {
                //回到顶部
                coroutineScope.launch {
                    scrollState.animateScrollToItem(0)
                }
            }) {
                Text("Scroll to the top")
            }
            Button(onClick = {
                //回到底部
                coroutineScope.launch {
                    scrollState.animateScrollToItem(listSize - 1)
                }
            }) {
                Text("Scroll to the end")
            }
        }
        LazyColumn(state = scrollState) {
            items(listSize) {
                ImageListItem(it)
            }
        }
    } 

 

posted @ 2021-12-06 11:00  勤奋的小铁  阅读(1266)  评论(0编辑  收藏  举报