Kotlin OnTouchListener called, but it does not cancel performClick

How to override checkClick in Kotlin to avoid a 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.

0
android kotlin ontouchlistener
Nov 08 '17 at 1:23
source share
1 answer

Ok, I solved my 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 so I can call single onTouch and implement it for many buttons, and also use onClick by:

 view.performClick() 

Do not forget to implement:

 View.OnTouchListener 

And set the listener:

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

Thank you, Lord! :)

0
Nov 08 '17 at 3:05
source share
— -



All Articles