Hi, how do I reduce swipe sensitivity in Horizonta...
# compose
u
Hi, how do I reduce swipe sensitivity in HorizontalPager? For e.g. in Instagram, even with a gentle flick the image carousel reponds and slides to next image. However, with the HorizontalPager, it has to be significant swipe gesture to able to move to another image. Is there any way to reduce this sensitivity?
2
Found it:
Copy code
@OptIn(ExperimentalSnapperApi::class)
@Composable
fun flingBehavior(
    pagerState: PagerState,
    noOfPages: Int,
    minFlingDistanceDp: Dp
): FlingBehavior {
    var currentPageIndex by rememberSaveable { mutableStateOf(pagerState.currentPage) }

    return PagerDefaults.flingBehavior(
        state = pagerState,
        snapIndex = { layoutInfo, _, _ ->
            val distanceToStartSnap = layoutInfo.distanceToIndexSnap(currentPageIndex)
            currentPageIndex = when {
                distanceToStartSnap < -(minFlingDistanceDp.value) -> {
                    (currentPageIndex + 1).coerceAtMost(noOfPages - 1)
                }
                distanceToStartSnap > minFlingDistanceDp.value -> {
                    (currentPageIndex - 1).coerceAtLeast(0)
                }
                else -> {
                    currentPageIndex
                }
            }
            currentPageIndex
        }
    )
}
The custom flingBehavior above can be configured with
minFlingDistanceDp
which I set to 5.dp for high sentivity.
187 Views