How can I disable the horizontal stretching/overscroll when using the `verticalScroll` modifier?

744 Views Asked by At

I want a scrollable view which I'm doing by using the verticalScroll modifier on a column.

Column(modifier = Modifier.verticalScroll(rememberScrollState())) { 
    // ... I could provide more detail about the contents here but I don't think it matters.
}

When I start scrolling vertically, the view will allow horizontal over scroll and stretch the view horizontally. This doesn't seem like desirable behavior to me.

Scrolling around in my view

Is there anyway to stop this behavior from happening?

1

There are 1 best solutions below

0
On

You can try disabling the overscroll via CompositionLocal directly scoping the Column

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun MyListWithoutOverScroll() {
    CompositionLocalProvider(
        LocalOverscrollConfiguration provides null
    ) {
        Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
            repeat(100) {
                Text("Hello World $it")
            }
        }
    }
}

or set it in Activity level

@Composable
fun MyList() {
    Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
        repeat(100) {
            Text("Hello World $it")
        }
    }
}


@OptIn(ExperimentalFoundationApi::class)
@AndroidEntryPoint
class MyActivity : ComponentActivity() {

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

        setContent {
            CompositionLocalProvider(
                LocalOverscrollConfiguration provides null
            ) {
                MyList()
            }
        }
    }
}

Without overscroll using CompositionLocalProvider setting it to null (not-stretching)

enter image description here

With overscroll (stretching)

enter image description here