尊重 MotionEvent.ACTION_SCROLL 的 LazyColumn

发布于 2025-01-11 20:40:39 字数 103 浏览 1 评论 0原文

如何强制 compose' LazyColumn 表现得像 RecyclerView 或 ListView 等传统可滚动元素一样?

当想要用鼠标滚动时很有用,例如用 vysor。

How to force compose' LazyColumn to act like traditional scrollable elements like RecyclerView or ListView?

Useful when want to scroll with mouse, e.g. with vysor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2025-01-18 20:40:39

解决办法是在修改器中添加filter

const val VERTICAL_SCROLL_MULTIPLIER = -4

// Helps with scrolling with mouse wheel (just like recycler view/list view without compose)
@ExperimentalComposeUiApi
@Composable
fun MyLazyColumn(
    modifier: Modifier = Modifier,
    state: LazyListState, // rememberLazyListState()
    content: LazyListScope.() -> Unit
) {
    LazyColumn(
        state = state,
        modifier = modifier
            .pointerInteropFilter {
                if (it.action == MotionEvent.ACTION_SCROLL) {
                    state.dispatchRawDelta(it.getAxisValue(MotionEvent.AXIS_VSCROLL) * VERTICAL_SCROLL_MULTIPLIER)
                }
                false
            },
        content = content
    )
}

The solution is to add filter to modifier

const val VERTICAL_SCROLL_MULTIPLIER = -4

// Helps with scrolling with mouse wheel (just like recycler view/list view without compose)
@ExperimentalComposeUiApi
@Composable
fun MyLazyColumn(
    modifier: Modifier = Modifier,
    state: LazyListState, // rememberLazyListState()
    content: LazyListScope.() -> Unit
) {
    LazyColumn(
        state = state,
        modifier = modifier
            .pointerInteropFilter {
                if (it.action == MotionEvent.ACTION_SCROLL) {
                    state.dispatchRawDelta(it.getAxisValue(MotionEvent.AXIS_VSCROLL) * VERTICAL_SCROLL_MULTIPLIER)
                }
                false
            },
        content = content
    )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文