-1

I'm working right now on an little app to test arround with motion and touch events. I am facing the problem that my Listener class has to be abstracted but I cannot create an object from that listener which I would need to put it on the "setOnTouchListener" method of an imageView.

Listener class:

abstract class GestureListener(directionDisplayer: TextView) : View.OnTouchListener,` GestureDetector.OnGestureListener {

    private var directionDisplayer: TextView = directionDisplayer

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        val gestureDetector = GestureDetector(this)
        gestureDetector.onTouchEvent(event)
        return true
    }

    override fun onFling(
        downEvent: MotionEvent?,
        moveEvent: MotionEvent?,
        velocityX: Float,
        velocityY: Float
    ): Boolean {
        var result = false

        if (downEvent != null && moveEvent != null) {
            var diffY: Float = moveEvent.y - downEvent.y
            var diffX: Float = moveEvent.x - downEvent.x

            val SWIPE_MIN = 100
            val SWIPE_Velocity = 100 //TODO WIDTH

            if (Math.abs(diffX) > Math.abs(diffY)) {
                //RIGHT OR LEFT
                if (Math.abs(diffX) > SWIPE_MIN && Math.abs(velocityX) > SWIPE_Velocity) {
                    if (diffX > 0) {
                        swipeRight()
                    } else {
                        swipeLeft()
                    }
                    result = true
                }
            } else {
                //UP OR DOWN
                if(Math.abs(diffY) > SWIPE_MIN && Math.abs(velocityY) > SWIPE_Velocity) {
                    if(diffY > 0) {
                        swipeUp()
                    } else {
                        swipeDown()
                    }
                    result = true
                }


            }

        }
        return result
    }

    private fun swipeDown() {
        directionDisplayer.text = "Direction: DOWN"
    }

    private fun swipeUp() {
        directionDisplayer.text = "Direction: UP"
    }

    private fun swipeLeft() {
        directionDisplayer.text = "Direction: LEFT"
    }

    private fun swipeRight() {
        directionDisplayer.text = "Direction: RIGHT"
    }

MainActivity (only the important):

    class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )

        setContentView(R.layout.activity_main)
        val canvasImage: ImageView = findViewById(R.id.canvas)

        canvasImage.setOnTouchListener(GestureListener(findViewById(R.id.showDirection)))

Does anybody have an idea how to fix this?

i.bondarenko
  • 3,442
  • 3
  • 11
  • 21
Schmidi
  • 3
  • 5
  • If it has to be abstract that you have to pass object that implements GestureListener in your canvasImage.setOnTouchListener line – Ikazuchi Sep 27 '19 at 08:48
  • Do you mean like this? "object:GestureListener(...)" – Schmidi Sep 27 '19 at 08:52
  • Yes, just like this, and you can do object:GestureListener(){//there you can override methods} – Ikazuchi Sep 27 '19 at 08:53
  • Well it's a bit different in Kotlin so I think it should be fine also to help people that have their first programming language – Schmidi Sep 27 '19 at 09:12

1 Answers1

0

GestureListener is an abstract class. So, you have to implement the members of iew.OnTouchListener and GestureDetector.OnGestureListener not yet implemented either in the abstract class itself or in your code like this-

    canvasImage.setOnTouchListener(object : GestureListener(textView) {
        override fun onShowPress(e: MotionEvent?) {
            TODO("not implemented")
        }

        override fun onSingleTapUp(e: MotionEvent?): Boolean {
            TODO("not implemented")
        }

        override fun onDown(e: MotionEvent?): Boolean {
            TODO("not implemented")
        }

        override fun onScroll(
            e1: MotionEvent?,
            e2: MotionEvent?,
            distanceX: Float,
            distanceY: Float
        ): Boolean {
            TODO("not implemented")
        }

        override fun onLongPress(e: MotionEvent?) {
            TODO("not implemented")
        }
    })
Gulshan
  • 3,611
  • 5
  • 35
  • 46
  • Thanks for your helpfull answer already. Just got a little bit confused... as you can see I wanted to check if a person swiped up, right, left, down in my listener. But now these implemented methods are only triggered and idk if I used now the right events at all in my listener class or how to connect my self made code from my listener into these implemented events – Schmidi Sep 27 '19 at 09:07