Jetpack Compose LazyColumn 以编程方式滚动到 Item [英] Jetpack Compose LazyColumn programmatically scroll to Item
本文介绍了Jetpack Compose LazyColumn 以编程方式滚动到 Item的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法以编程方式将 LazyColumn
滚动到列表中的某个项目?我认为可以通过提升 LazyColumn
参数 state: LazyListState = rememberLazyListState()
来完成,但我不知道如何更改此状态,例如点击按钮.
解决方案
With 1.0.x
LazyListState
支持滚动位置通过>
scrollToItem()
函数,立即"捕捉滚动位置,animateScrollToItem()
使用动画滚动
类似于:
val listState = rememberLazyListState()//记住一个 CoroutineScope 才能启动val coroutineScope = rememberCoroutineScope()LazyColumn(state = listState) {//...}按钮 (onClick = {coroutineScope.launch {//动画滚动到第 10 项listState.animateScrollToItem(index = 10)}}){文本(点击")}
Is there any way to programmatically scroll LazyColumn
to some item in the list? I thought that it can be done by hoisting the LazyColumn
argument state: LazyListState = rememberLazyListState()
but I have no idea how I can change this state e.g. on Button click.
解决方案
With 1.0.x
LazyListState
supports the scroll position via
- the
scrollToItem()
function, which ‘immediately’ snaps the scroll position, animateScrollToItem()
which scrolls using an animation
Something like:
val listState = rememberLazyListState()
// Remember a CoroutineScope to be able to launch
val coroutineScope = rememberCoroutineScope()
LazyColumn(state = listState) {
// ...
}
Button (
onClick = {
coroutineScope.launch {
// Animate scroll to the 10th item
listState.animateScrollToItem(index = 10)
}
}
){
Text("Click")
}
这篇关于Jetpack Compose LazyColumn 以编程方式滚动到 Item的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文