Ric*_*per 2 android state android-jetpack-compose android-jetpack-compose-list android-jetpack-compose-modifier
这个问题以前已经被问过,但以不同的形式,关于一些特定的用例,到目前为止还没有答案。我终于让它工作了,所以我在这里分享这个,但是这不应该被标记为重复,因为前面的所有问题都指定了特定的东西,比如Columnswith scrollable Modifiers, or LazyRows,但这将解决一般的所有问题,我的意思是所有惰性滚动器,甚至希望甚至是可滚动的容器。我将发布答案,这只是我希望与社区分享的一段知识,当然,欢迎任何改进。
这是完整的工作解决方案:-
@Composable
fun DUME() {
val stateRowX = rememberLazyListState() // State for the first Row, X
val stateRowY = rememberLazyListState() // State for the second Row, Y
Column { // Placing two Lazy Rows one above the other for the example
LazyRow(state = stateRowY) {
items(LoremIpsum(10).values.toList()) {
Text(it)
}
}
LazyRow(state = stateRowX) {
items(LoremIpsum(10).values.toList()) {
Text(text = it)
}
}
}
//This might seem crazy
LaunchedEffect(stateRowX.firstVisibleItemScrollOffset) {
stateRowY.scrollToItem(
stateRowX.firstVisibleItemIndex,
stateRowX.firstVisibleItemScrollOffset
)
}
LaunchedEffect(stateRowY.firstVisibleItemScrollOffset) {
stateRowX.scrollToItem(
stateRowY.firstVisibleItemIndex,
stateRowY.firstVisibleItemScrollOffset
)
}
}
Run Code Online (Sandbox Code Playgroud)
这里的导入items是 : androidx.compose.foundation.lazy.items,它接受一个列表而不是数字(大小)。
| 归档时间: |
|
| 查看次数: |
2423 次 |
| 最近记录: |