2

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



   
John
  • 5,581
  • 6
  • 29
  • 46

1 Answers1

2

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

Johann
  • 27,536
  • 39
  • 165
  • 279
  • Thanks Johann. Due to input events being consumed by a pointerInteropFilter before they hit the Composable, and due to there being no way to stop a pointerInteropFilter from consuming input events, I have to scroll via the ScrollState using old fashioned MotionEvents. (I'm forced to use the pointerInteropFilter because nothing else in Jetpack Compose captures input pressure.) – John Nov 08 '21 at 10:57
  • NestedScrolling is the answer! It turns out that adding a nestScrollConnection to a composable that also has a PointerInteropFilter allows the PointerInteropFilter to NOT consume all its MotionEvents. Who knew? – John Nov 17 '21 at 11:47