3

I am using Accompanist Pager library (version 0.18.0) and created a VerticalPager for displaying certain pictures.

While scrolling up/down, the required behavior is to scroll to the next/previous picture, which works, but on a quick scroll, it is snapping to the same picture again.

I want to switch to the pictures on slightest scroll.

I tried to modify the. flingBehaviour as mentioned here: How to set a threshold for LazyColumn/ViewPager before starting to scroll?

but it is not working as expected.

Ali_Waris
  • 1,944
  • 2
  • 28
  • 46

1 Answers1

0

Short Answer

Disable overscroll

CompositionLocalProvider(
    LocalOverscrollConfiguration provides null
) {
    ViewPager()
}

Long Answer

I believe it's an issue with SnappingFlingBehavior that's used in Pager.kt

I ran into the same issue, I couldn't find an answer to this but after looking into the source for Scrollable.kt in the onDragStopped() function we can see this check at the top

val preOverscrollConsumed =
        if (overscrollEffect != null && overscrollEffect.isEnabled) {
            overscrollEffect.consumePreFling(axisVelocity.toVelocity()).toFloat()
        } else {
            0f
        }

If the overscroll is enabled -- which is the bounce like effect you're seeing at the beginning/end of a list and in the case of quick scrolling on the same item, it takes away from the velocity.

This results in the implementation of SnappingFlingBehavior.performFling() and the internal performSpringFling calculation of the targetIndex (your index you want to scroll to in your ViewPager) being the same as your current index thus resulting in no movement.

If you disable overscroll, you'll get your intended behavior but a better solution would be to implement your own FlingBehavior

Pztar
  • 4,274
  • 6
  • 33
  • 39