Kotlin OnTouchListener called but it does not over

2019-03-28 11:20发布

问题:

How to override performClick in Kotlin to avoid warning.

next.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
        when (motionEvent.action){
            MotionEvent.ACTION_DOWN -> {
                val icon: Drawable = ContextCompat.getDrawable(activity.applicationContext, R.drawable.layer_bt_next)
                icon.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY)
                next.setImageDrawable(icon)
            }
            MotionEvent.ACTION_UP -> {
                //view.performClick()
                next.setImageResource(R.drawable.layer_bt_next)
            }
        }
        return@OnTouchListener true
    })

view.performClick does not work.

回答1:

Try this way :

 next.setOnTouchListener(object : View.OnTouchListener {
        override fun onTouch(v: View?, event: MotionEvent?): Boolean {
            when (event?.action) {
                MotionEvent.ACTION_DOWN -> //Do Something
            }

            return v?.onTouchEvent(event) ?: true
        }
    })


回答2:

I don't think your solution will actually solve them problem presented by the warning. The warning states that certain accessibility functions use performClick() to activate buttons. If you look in the View class, the performClick() funtions calls the onClickListener directly, meaning the code in the onTouchListener will not be executed (next.setImageResource(R.drawable.layer_bt_next)) for these accessibility functions, since the view will never be physically touched, and thus your onTouch code won't run. You have to do one of either:

  1. Subclass the view you are setting the onTouchListener on, and override performClick to execute the code, or
  2. Set an onClickListener on the view that executes the code.

You could just implement onClickListener in your onTouchListener class and manually call onClick() from your onTouchListener (where you have view.performClick() now), and then move your executable code to the onClick override. You would also have to set BOTH onTouchListener and onClickListener on your views.



回答3:

Okay, I solved my own problem by overriding the OnTouch listener.

override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
    when (view) {
        next -> {
            Log.d("next", "yeyy")
            when (motionEvent.action){
                MotionEvent.ACTION_DOWN -> {
                    val icon: Drawable = ContextCompat.getDrawable(activity.applicationContext, R.drawable.layer_bt_next)
                    icon.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY)
                    next.setImageDrawable(icon)
                }
                MotionEvent.ACTION_UP -> {
                    view.performClick()
                    next.setImageResource(R.drawable.layer_bt_next)
                }
            }
        }
        previous -> {
            //ingredients here XD
        }
    }
    return true
}

And in that way, I can call single onTouch and implement it to many button and also can use the onClick by :

view.performClick()

Don't forget to implement :

View.OnTouchListener

And set the listener :

next.setOnTouchListener(this)
previous.setOnTouchListener(this)


回答4:

I'm not sure this is the same issue you saw, but since I found this page searching for my issue, I thought I'd add my experience to help others :)

In my case the warning was being generated because the nullable view could have been of type Void. Calling the following:

nullableView?.setOnTouchListener(this)

produced the error:

Custom view Void has setOnTouchListener called on it but does not override performClick

Performing a null check and casting to a View before setting the listener solved for me in this case, since View will override performClick:

if (nullableView != null) (nullableView as View).setOnTouchListener(this)