1

这个问题以前有人问过,但形式不同,关于一些具体的用例,至今没有答案。我终于让它工作了,所以我在这里分享这个,但这不应该被标记为重复,因为前面的所有问题都指定了特定的东西,比如Columnswith scrollable Modifiers, or LazyRows,但这通常会解决所有问题,我的意思是所有懒惰的滚动器,希望甚至是一般的可滚动容器。我会发布答案,所以这只是我希望与社区分享的一个知识,当然,欢迎任何改进。

4

1 回答 1

2

这是完整的工作解决方案:-

@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
        )
    }
}

这里的items导入是 : androidx.compose.foundation.lazy.items,它接受一个列表而不是一个数字(大小)。

于 2021-10-17T20:54:04.853 回答