On Android using Jetpack Compose, I want to make the HorizontalPager scroll in only one direction, so forward only. I can’t get either pointerInput
or pointerInteropFilter
to correctly handle my gesture detection.
I’ve tried the following code, but neither option works
// if you use detectDragGesture on the box, then the message is printed, however the event does not bubble up and the horizontalPager will not swipe
// if you put the pointerInput onto the HorizontalPager, then the gesture is not detected and no message is printed
HorizontalPager(state = pagerState) { page ->
// Display each page with a different background color
Box(
modifier = Modifier
.fillMaxSize()
.background(colors[page])
.pointerInput(Unit) {
detectDragGestures { _, _ ->
println("In Here")
}
}
) {
}
}
// the other option is to use pointerInteropFilter
// the problem with this is that when used with the horizontalPager it does not detect the ACTION_MOVE for horizontal scroll
HorizontalPager(state = pagerState) { page ->
Box(
modifier = Modifier
.fillMaxSize()
.background(colors[page])
.pointerInteropFilter { motionEvent ->
when (motionEvent.action)
MotionEvent.ACTION_MOVE -> {
// This is not detected for horizontal scroll
println("ACTION_MOVE")
} }
true
}
) {
Text("Hello")
}
}
If I can correctly detect a backward swipe then I should be able to set userScrollEnabled
on the pager to disable the backward swipe.
Any other approach to disabling backward swiping would also work.
Recognized by Mobile Development Collective