试图设置一个抽象的监听器

时间:2019-09-27 07:49:03

标签: java android kotlin listener motion

我现在正在使用一个小应用程序来测试运动和触摸事件的周围环境。我面临的问题是必须抽象我的侦听器类,但是我无法从该侦听器创建对象,因此需要将其放在imageView的“ setOnTouchListener”方法上。

监听器类:

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)))

有人知道如何解决此问题吗?

1 个答案:

答案 0 :(得分:0)

python-pptx是一个抽象类。因此,您必须实现尚未在抽象类本身或这样的代码中实现的GestureListeneriew.OnTouchListener的成员-

GestureDetector.OnGestureListener