0

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.

Mirco
  • 67
  • 10
  • Hi Mirco, why is not working?, touch listener is a good practice to implement swipe feature. You can share us some code and we can take a look – isthemartin Sep 08 '20 at 20:43
  • Have you found a solution to this? I have the same problem, and setOnTouchListener doesn’t work with recyclerview – oscarabilleira Jan 18 '21 at 22:13
  • Hi, haven't found a good solution. – Mirco Jan 20 '21 at 10:06
  • 1
    I have found this solution that works for me https://mobikul.com/applying-touch-listener-recycler-view-can-perform-click-whole-item-view/ (I just had to put the onclick event in this code and delete from my previous position) – oscarabilleira Jan 24 '21 at 10:29

0 Answers0