Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android kotlin onTouchListener wants me to override performClick()

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?

like image 211
just_user Avatar asked Sep 12 '17 08:09

just_user


1 Answers

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

like image 144
lambda Avatar answered Sep 25 '22 12:09

lambda