34.1 Intercepting Touch Events
Touch events can be intercepted by a view object through the registration of an onTouchListener event listener and the implementation of the corresponding onTouch() callback method or lambda. The following code, for example, ensures that any touches on a ConstraintLayout view instance named myLayout result in a call to a lambda expression:
binding.myLayout.setOnTouchListener {v: View, m: MotionEvent ->
// Perform tasks here
true
}
Of course, the above code could also be implemented by using a function instead of a lambda as follows, though the lambda approach results in more compact and readable code:
binding.myLayout.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View, m: MotionEvent): Boolean {
...