gpt4 book ai didi

android - Jetpack Compose 滚动 LazyColumn with CoroutineScope 结果错误 A MonotonicFrameClock 在此 CoroutineContext 中不可用

转载 作者:行者123 更新时间:2023-12-04 23:55:58 54 4
gpt4 key购买 nike

退房this example使用状态和协程作为列表滚动

@Composable
fun ScrollingList() {
val listSize = 100
// We save the scrolling position with this state
val scrollState = rememberLazyListState()
// We save the coroutine scope where our animated scroll will be executed
val coroutineScope = rememberCoroutineScope()

Column {
Row {
Button(onClick = {
coroutineScope.launch {
// 0 is the first item index
scrollState.animateScrollToItem(0)
}
}) {
Text("Scroll to the top")
}

Button(onClick = {
coroutineScope.launch {
// listSize - 1 is the last index of the list
scrollState.animateScrollToItem(listSize - 1)
}
}) {
Text("Scroll to the end")
}
}

LazyColumn(state = scrollState) {
items(listSize) {
ImageListItem(it)
}
}
}
}
哪个适用于暂停功能
suspend fun animateScrollToItem(
/*@IntRange(from = 0)*/
index: Int,
/*@IntRange(from = 0)*/
scrollOffset: Int = 0
) {
doSmoothScrollToItem(index, scrollOffset)
}
如果我将协程范围更改为
val coroutineScope = CoroutineScope(Dispatchers.Main)
它返回

java.lang.IllegalStateException: A MonotonicFrameClock is notavailable in this CoroutineContext. Callers should supply anappropriate MonotonicFrameClock using withContext.


这是什么意思,是 rememberCoroutineScope()提供 的唯一途径协程范围 到这个功能?

最佳答案

由于 animateScrollToItem 是可组合函数,因此需要在组合范围内调用它。
As documentation states

rememberCoroutineScope is a composable function that returns a CoroutineScope bound to the point of the Composition where it's called. The scope will be cancelled when the call leaves the Composition.

关于android - Jetpack Compose 滚动 LazyColumn with CoroutineScope 结果错误 A MonotonicFrameClock 在此 CoroutineContext 中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68398064/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com