问题
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