一起滚动两个懒惰的滚动条 [英] Scroll Two Lazy Scrollers Together

本文介绍了一起滚动两个懒惰的滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题以前有人问过,但形式不同,针对一些特定的用例,到目前为止还没有答案.我终于让它工作了,所以我在这里分享这个,但这不应该被标记为重复,因为之前的所有问题都指定了特定的东西,比如 Columnsscrollable ModifiersLazyRows,但这将解决一般的所有问题,我的意思是所有惰性滚动器,希望甚至是可滚动的容器.我会发布答案,所以这只是我希望与社区分享的一部分知识,当然,也欢迎任何改进.

This question has been asked before, but in different forms, regarding some specific use cases, and so far there has been no answer. I finally got it working, so I am sharing this here, but this should not be marked as a duplicate since all the previous questions specify specific stuff, like Columns with scrollable Modifiers, or LazyRows, but this will resolve all the issues in general, I mean all the lazy-scrollers, and hopefully even scrollable containers in general. I'll post the answer so this is just a piece of knowledge that I wished to share with the community, also, any improvements are welcome, of course.

推荐答案

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

This is the full working solution:-

@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,它接受一个列表而不是一个数字(大小).

The items import here is : androidx.compose.foundation.lazy.items, this accepts a list instead of a number (the size).

这篇关于一起滚动两个懒惰的滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆