【问题标题】:How to disable scrolling in LazyColumn and Column如何在 LazyColumn 和 Column 中禁用滚动
【发布时间】:2022-01-15 06:37:09
【问题描述】:

我想在我的 LazyColumn 或 Column 中禁用滚动。

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

Modifier.verticalScroll(...)

不起作用。

这是我的代码:

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

【问题讨论】:

标签: android kotlin android-jetpack-compose android-jetpack lazycolumn


【解决方案1】:

一种简单的方法是将 LazyColumn 放置在包含另一个 Box 的 Box 中。可以组合嵌套的 Box 来拦截滚动,从而防止 LazyColumn 接收任何滚动事件。要启用滚动,只需阻止添加嵌套的 Box。至于禁用列中的滚动,这是默认设置。默认情况下,列没有滚动:

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())) {}
                    }
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 2021-12-19
    • 1970-01-01
    • 2012-04-23
    • 2019-02-12
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多