Implement Swipe Gesture in Kotlin
Asked Answered
K

1

15

I'm trying to implement a gesture detector for my application written in Kotlin. I'm following the comment on this question: link

So, I created the OnSwipeTouchListener class and I implemented the listener in my class:

class DetailActivity : AppCompatActivity() {

override fun onBackPressed() {
    super.onBackPressed()
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.detail_activity)
    window.decorView.setOnTouchListener(object: OnSwipeTouchListener(this@DetailActivity) {
        override fun onSwipeLeft() {
            onBackPressed()
        }
        override fun onSwipeRight() {
            onBackPressed()
        }
    })
}

The problem is that it doesn't get recognized and I don't get any error. I tried to put a Log.i to check if the program enters in the two overrides methods, but nothing get printed. Can it be a View problem?

EDIT: this is the Listener code:

    open class OnSwipeTouchListener(ctx: Context) : OnTouchListener {

    private val gestureDetector: GestureDetector

    companion object {

        private val SWIPE_THRESHOLD = 100
        private val SWIPE_VELOCITY_THRESHOLD = 100
    }

    init {
        gestureDetector = GestureDetector(ctx, GestureListener())
    }

    override fun onTouch(v: View, event: MotionEvent): Boolean {
        return gestureDetector.onTouchEvent(event)
    }

    private inner class GestureListener : SimpleOnGestureListener() {


        override fun onDown(e: MotionEvent): Boolean {
            return true
        }

        override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
            var result = false
            try {
                val diffY = e2.y - e1.y
                val diffX = e2.x - e1.x
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight()
                        } else {
                            onSwipeLeft()
                        }
                        result = true
                    }
                } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom()
                    } else {
                        onSwipeTop()
                    }
                    result = true
                }
            } catch (exception: Exception) {
                exception.printStackTrace()
            }

            return result
        }


    }

    open fun onSwipeRight() {}

    open fun onSwipeLeft() {}

    open fun onSwipeTop() {}

    open fun onSwipeBottom() {}
}
Kannada answered 17/3, 2020 at 18:55 Comment(4)
It seems like setting onTouchListener to the decor view can be a problem. Have you tried setting OnSwipeTouchListener on your activity root view or other view?Kanpur
Yep, I tried it on the other two main activities and I have the same problem. I've also tried to implement it in my four fragments but I'm getting error when I pass the context as parameter.Kannada
can you post swipe listener codeHaugh
@Haugh I updated the question with the codeKannada
C
11

I tried your code like this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main">

</androidx.constraintlayout.widget.ConstraintLayout>

and

main.setOnTouchListener(object: OnSwipeTouchListener(this@Main3Activity) {
        override fun onSwipeLeft() {
            onBackPressed()
        }
        override fun onSwipeRight() {
            onBackPressed()
        }
    })

It works. Probably the problem is decorview

Cleric answered 19/3, 2020 at 19:57 Comment(2)
what is the main variable here?Ereshkigal
I believe it's ID of the ConstraintlayoutFaydra

© 2022 - 2024 — McMap. All rights reserved.