0

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

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

或者

修饰符.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)
                }
            }
    }
4

1 回答 1

0

一种简单的方法是将 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-12-10T16:19:27.840 回答