简体   繁体   English

如何在 LazyColumn 和 Column 中禁用滚动

[英]How to disable scrolling in LazyColumn and Column

I want to disable scrolling in my LazyColumn or Column.我想在我的 LazyColumn 或 Column 中禁用滚动。

Modifier.scrollable(state = rememberScrollState(), enabled = false, orientation = Orientation.Vertical) Modifier.scrollable(state = rememberScrollState(), enabled = false, orientation = Orientation.Vertical)

or要么

Modifier.verticalScroll(...) Modifier.verticalScroll(...)

doesnt work.不工作。

Here is my Code:这是我的代码:

Column(
        modifier = Modifier
            .fillMaxSize()
        ) {
        Box(
            modifier = Modifier
                .padding(15.dp)
                .height(60.dp)
                .clip(RoundedCornerShape(30))
        ) {
            TitleSection(text = stringResource(id = R.string...))
        }
            LazyColumn(
                contentPadding = PaddingValues(start = 7.5.dp, end = 7.5.dp, bottom = 100.dp),
                modifier = Modifier
                    .fillMaxHeight()
            ) {
                items(categoryItemContents.size) { items ->
                    CategoryItem(categoryItemContents[items], navController = navController)
                }
            }
    }

A simple approach is to place the LazyColumn inside a Box that contains another Box.一种简单的方法是将 LazyColumn 放在包含另一个 Box 的 Box 中。 The nested Box can be composed to intercept scrolling, thus preventing the LazyColumn from receiving any scrolling events.可以组合嵌套的 Box 来拦截滚动,从而防止 LazyColumn 接收任何滚动事件。 To enable scrolling, just prevent the nested Box from being added.要启用滚动,只需阻止添加嵌套的 Box。 As for disabling scrolling in a Column, that is the default.至于禁用列中的滚动,这是默认设置。 Columns by default don't have scrolling:默认情况下,列没有滚动:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        startActivity(intent)

        setContent {
            var scrollingEnabled by remember { mutableStateOf(true) }

            Column() {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Text("Scrolling Enabled")

                    Switch(
                        checked = scrollingEnabled,
                        onCheckedChange = { scrollingEnabled = it }
                    )
                }

                Box(modifier = Modifier.fillMaxSize()) {
                    LazyColumn(Modifier.fillMaxWidth(), state = rememberLazyListState()) {
                        items((1..100).toList()) {
                            Text("$it")
                        }
                    }

                    if (!scrollingEnabled) {
                        Box(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())) {}
                    }
                }
            }
        }
    }
}

With the LazyColumn you can use the userScrollEnabled parameter to disable the scrolling.使用LazyColumn ,您可以使用userScrollEnabled参数来禁用滚动。

LazyColumn(
    state = state,
    userScrollEnabled = false
){
   //items
}

Note that you can still scroll programmatically using the state even when it is disabled.请注意,您仍然可以使用 state 以编程方式滚动,即使它已被禁用。

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

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