简体   繁体   English

如何防止在 Jetpack Compose 中向惰性列/行的一个方向滑动?

[英]How to prevent swipe one direction of Lazy Column/Row in Jetpack Compose?

For example i have a lazy Column;例如我有一个懒惰的专栏;

Box(
    modifier = Modifier.fillMaxSize(),
    contentAlignment = Alignment.Center
) {
    LazyColumn(Modifier.height(100.dp).border(2.dp, Color.Red)) {
        items(20){
            Text(text = "Swipe")
        }
    }
}

View;看法;

在此处输入图像描述

I want to swipe only down not up.我只想向下滑动而不是向上滑动。 How can i achieve this?我怎样才能做到这一点?

You can create a NestedScrollConnection and consume the upward scroll in there.您可以创建一个NestedScrollConnection并在其中使用向上滚动。 Something like this:像这样的东西:

val nestedScrollConnection = remember {
    object : NestedScrollConnection {
        override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
            val delta = available.y
            return if (delta < 0) {
                Offset.Zero
            } else {
                Offset(available.x, available.y)
            }
        }
    }
}

Then just assign it to your LazyColumn modifier :然后只需将其分配给您的LazyColumn modifier

LazyColumn(Modifier.height(100.dp).border(2.dp, Color.Red).nestedScroll(nestedScrollConnection)) {
        items(20){
            Text(text = "Swipe")
        }
    }

Flip the if condition in the onPreScroll method if you want to block scroll in the other direction.如果要阻止向另一个方向滚动,请翻转onPreScroll方法中的 if 条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM