I found a lot of examples for swipe detection on a recyclerview item. But I try to detect right/left swipe on the whole view.
There are also examples like this Android: How to handle right to left swipe gestures on how to detect swipe on views like layout container. But attaching this touch listener to the recyclerview is not working.
So how can I detect left / right swipe on a whole recyclerview?
Edit for SmartSwipe Lib: I have tried to add the https://github.com/luckybilly/SmartSwipe Lib because the effect of swipe looks really nice.
But if I try to wrap it to the recyclerview I see no data only the swipe effect:
SmartSwipe.wrap(binding.recyclerView).addConsumer(BezierBackConsumer())
.enableHorizontal()
.addListener(object : SimpleSwipeListener() {
override fun onSwipeOpened(wrapper: SmartSwipeWrapper, consumer: SwipeConsumer, direction: Int) {
}
})
Attaching it to the constraint layout container has no effect at all.
Edit 2: Attaching an onTouchListener to the view doesn't work for the recyclerview:
view.setOnTouchListener(object : OnHorizontalSwipeListener(requireContext()) {
override fun onRightSwipe() {
println("Swipe right")
}
override fun onLeftSwipe() {
println("Swipe left")
}
})
Attaching it to the Recyclerview leads to an exception:
binding.recyclerView.setOnTouchListener.....
E/MessageQueue-JNI: java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter e1
at ui.OnHorizontalSwipeListener$GestureListener.onFling(Unknown Source:2)
Edit 3: Seems to work if I add null check to the listener class like this:
override fun onFling(
e1: MotionEvent?,
e2: MotionEvent?,
velocityX: Float,
velocityY: Float
): Boolean {
var result = false
try {
if (e1 != null && e2 != null) {
val diffY = e2.y - e1.y
val diffX = e2.x - e1.x
....
But starts working only after some scrolling. trying to swipe on a new initialized fragment doesn't work.