Android kotlin onTouchListener wants me to override performClick()

半世苍凉 提交于 2020-04-13 06:14:40

问题


I'm trying to get rid of a warning where Android Studio wants my onTouchListener to override performClick which I do, but the warning remains.

draggableBar!!.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
    when (motionEvent.getAction()) {
        MotionEvent.ACTION_DOWN -> {

        }
        MotionEvent.ACTION_UP -> {
            view.performClick()
        }
    }

    return@OnTouchListener true
})

Could this be an Android Studio bug or am I doing something wrong?


回答1:


Okay, I have the same problem but i fixed it by overriding the onTouch listener.

The default onTouch wants us to override performClick(), but this does not work even calling the method by view.performClick().

So therefore override your onTouch like this:

override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
    when (view) {
        draggableBar -> {
            when (motionEvent.getAction()) {
                MotionEvent.ACTION_DOWN -> {

                }
                MotionEvent.ACTION_UP -> {
                    view.performClick()
                }
            }
        }
        otherButtonHere -> {
            //your welcome
        }
    }

    return true
}

And in that way, you can use single onTouch() in all clickable views you have.

Don't forget to implement to your Class:

View.OnTouchListener

And set the listener:

draggableBar!!.setOnTouchListener(this)

HOPE IT HELPS! :)



来源:https://stackoverflow.com/questions/46171379/android-kotlin-ontouchlistener-wants-me-to-override-performclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!