Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you fling a ScrollState in Jetpack Compose?

For reasons that have to do with Jetpack Compose input modifiers consuming all MotionEvents, I find myself writing my own scroll routine for a Composable, of which I have access to the ScrollState.

I have figured out everything I need except flinging. I can't see how to apply performFling(initialVelocity) on a ScrollState. All I can find in the docs are ScrollState.scrollTo and ScrollState.scrollBy, which aren't so useful with flings, since the scroll destination or size is unknown.

I also can't find a ScrollState listener, similar to onScrollStateChanged(state: Int) in the old Android world, that fires when scrolling state changes.

Here is what I have in case somebody can point me in the right direction:

var lastY: Float? = null
var velocityTracker: VelocityTracker? = null

fun scroll(event: MotionEvent) {
    when (event.action) {
        MotionEvent.ACTION_DOWN -> {
            velocityTracker = VelocityTracker.obtain()
            lastY = event.y
        }
        MotionEvent.ACTION_UP -> {
            lastY = null
            velocityTracker?.let {
                it.computeCurrentVelocity(1000)
                val initialVelocity = it.yVelocity
                velocityTracker?.recycle()
                coroutineScope.launch {
                    ???? scrollState.PERFORMFLING?(initialVelocity) ????
                    AND THEN WHEN THE FLING IS FINISHED viewModel.scrollOffset = scrollState.value
                }
            }
        }
        else -> {
            velocityTracker?.addMovement(event)
            lastY?.let {
                val scrollAmount = it - event.y
                lastY = event.y
                coroutineScope.launch {
                    scrollState.scrollBy(scrollAmount)
                    viewModel.scrollOffset = scrollState.value
                }
            }
        }
    }
}



   
like image 955
John Avatar asked Jan 21 '26 16:01

John


1 Answers

You could try using a nestedScroll:

 val nestedScrollConnection = remember {
      object : NestedScrollConnection {
            override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
                 return super.onPostFling(consumed, available)
            }

            override suspend fun onPreFling(available: Velocity): Velocity {
                 return super.onPreFling(available)
            }
      }
 }
    
   
Column(modifier = Modifier
      .verticalScroll(rememberScrollState())
      .nestedScroll(nestedScrollConnection)) {

}

If this doesn't work, just search

cs.android.com

for theNestedScrollConnection and it should give you a hint on how to handle flinging in Compose. Maybe NestedScrollConnection is all you need since it provides support for scrolling as well. You probably can ditch your code and just use NestedScrollConnection. To see NestedScrollConnection in action, check out the demo:

https://github.com/JohannBlake/Jetmagic

like image 122
Johann Avatar answered Jan 24 '26 16:01

Johann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!