How to disable and enable scrolling in LazyColumn/LazyRow in Jetpack Compose?

d-feverx

I want to dynamically enable and disable scrolling programmatically in a LazyColumn.

There don't seem to be any relevant functions on LazyListState or relevant parameters on LazyColumn itself. How can I achieve this in Compose?

Ryan M

There's not (currently) a built-in way to do this, which is a reasonable feature request.

However, the scroll API is flexible enough that we can add it ourselves. Basically, we create a never-ending fake scroll at MutatePriority.PreventUserInput to prevent scrolling, and then use a do-nothing scroll at the same priority to cancel the first "scroll" and re-enable scrolling.

Here are two utility functions on LazyListState to disable/re-enable scrolling, and a demo of them both in action (some imports will be required, but Android Studio should suggest them for you).

Note that because we're taking control of scrolling to do this, calling reenableScrolling will also cancel any ongoing scrolls or flings (that is, you should only call it when scrolling is disabled and you want to re-enable it, not just to confirm that it's enabled).

fun LazyListState.disableScrolling(scope: CoroutineScope) {
    scope.launch {
        scroll(scrollPriority = MutatePriority.PreventUserInput) {
            // Await indefinitely, blocking scrolls
            awaitCancellation()
        }
    }
}

fun LazyListState.reenableScrolling(scope: CoroutineScope) {
    scope.launch {
        scroll(scrollPriority = MutatePriority.PreventUserInput) {
            // Do nothing, just cancel the previous indefinite "scroll"
        }
    }
}

@Composable
fun StopScrollDemo() {
    val scope = rememberCoroutineScope()
    val state = rememberLazyListState()
    Column {
        Row {
            Button(onClick = { state.disableScrolling(scope) }) { Text("Disable") }
            Button(onClick = { state.reenableScrolling(scope) }) { Text("Re-enable") }
        }
        LazyColumn(Modifier.fillMaxWidth(), state = state) {
            items((1..100).toList()) {
                Text("$it", fontSize = 24.sp)
            }
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to disable and enable the scrolling on android ScrollView?

From Dev

Nativescript: how to programmatically disable / enable ScrollView scrolling?

From Dev

How to disable scroll / scrolling on iOS devices (iPad, iPhone, Mac) with user-controlled toggle (enable / disable)

From Dev

How to handle navigation in Jetpack Compose?

From Dev

DIsable scrolling for listview and enable for whole layout

From Java

How to disable RecyclerView scrolling?

From Java

How to disable scrolling temporarily?

From Dev

How to disable scrolling in OSMdroid

From Dev

How to *disable* natural scrolling?

From Dev

How to enable/disable toolbar scrolling programmatically when using design support library

From Dev

How to disable HorizontalScrollView scrolling on button click and enable again on another button click?

From Dev

How to enable / disable a Preference?

From Dev

How to enable or disable services?

From Dev

How to enable or disable services?

From Dev

How to enable / disable a Preference?

From Dev

How to Enable and Disable Buttons

From Dev

How to enable/disable a checkbox

From Dev

How to enable scrolling with arrow keys?

From Dev

How to enable 'fling scrolling' in a TextView

From Dev

How to enable fast scrolling for a ListFragment?

From Java

How can I get onTouchEvent in Jetpack Compose?

From Dev

How to add Image in Android using Jetpack Compose

From Dev

How to convert TextUnit to Dp in Jetpack Compose?

From Dev

How to know if Text is visible on Jetpack Compose?

From Dev

How to disable scrolling of AppBarLayout in CoordinatorLayout?

From Dev

How to disable trackpad horizontal scrolling

From Dev

how to enable compose key in cinnamon?

From Dev

Can I disable/enable Scrolling in Google Chart with a Button?

From Java

How to enable/disable inputs in blazor

Related Related

  1. 1

    How to disable and enable the scrolling on android ScrollView?

  2. 2

    Nativescript: how to programmatically disable / enable ScrollView scrolling?

  3. 3

    How to disable scroll / scrolling on iOS devices (iPad, iPhone, Mac) with user-controlled toggle (enable / disable)

  4. 4

    How to handle navigation in Jetpack Compose?

  5. 5

    DIsable scrolling for listview and enable for whole layout

  6. 6

    How to disable RecyclerView scrolling?

  7. 7

    How to disable scrolling temporarily?

  8. 8

    How to disable scrolling in OSMdroid

  9. 9

    How to *disable* natural scrolling?

  10. 10

    How to enable/disable toolbar scrolling programmatically when using design support library

  11. 11

    How to disable HorizontalScrollView scrolling on button click and enable again on another button click?

  12. 12

    How to enable / disable a Preference?

  13. 13

    How to enable or disable services?

  14. 14

    How to enable or disable services?

  15. 15

    How to enable / disable a Preference?

  16. 16

    How to Enable and Disable Buttons

  17. 17

    How to enable/disable a checkbox

  18. 18

    How to enable scrolling with arrow keys?

  19. 19

    How to enable 'fling scrolling' in a TextView

  20. 20

    How to enable fast scrolling for a ListFragment?

  21. 21

    How can I get onTouchEvent in Jetpack Compose?

  22. 22

    How to add Image in Android using Jetpack Compose

  23. 23

    How to convert TextUnit to Dp in Jetpack Compose?

  24. 24

    How to know if Text is visible on Jetpack Compose?

  25. 25

    How to disable scrolling of AppBarLayout in CoordinatorLayout?

  26. 26

    How to disable trackpad horizontal scrolling

  27. 27

    how to enable compose key in cinnamon?

  28. 28

    Can I disable/enable Scrolling in Google Chart with a Button?

  29. 29

    How to enable/disable inputs in blazor

HotTag

Archive