TightPhysics
TightPhysics

Reputation: 319

How to disable scrolling in LazyColumn and Column

I want to disable scrolling in my LazyColumn or Column.

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

or

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

Upvotes: 3

Views: 4447

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363785

With the LazyColumn you can use the userScrollEnabled parameter to disable the scrolling.

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

Note that you can still scroll programmatically using the state even when it is disabled.

Upvotes: 3

Johann
Johann

Reputation: 29867

A simple approach is to place the LazyColumn inside a Box that contains another Box. The nested Box can be composed to intercept scrolling, thus preventing the LazyColumn from receiving any scrolling events. To enable scrolling, just prevent the nested Box from being added. 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())) {}
                    }
                }
            }
        }
    }
}

Upvotes: 1

Related Questions