touches

fun View.touches(handled: (MotionEvent) -> Boolean = { true }): Flow<MotionEvent>

Create a Flow of touch events on the View instance.

Parameters

handled

function to be invoked with each value to determine the return value of the underlying View.OnTouchListener. Note that the Flow will only emit when this function evaluates to true.

Note: Values emitted by this flow are mutable and part of a shared object pool and thus are not safe to cache or delay reading (such as by observing on a different thread). If you want to cache or delay reading the items emitted then you must map values through a function which calls MotionEvent.obtain or MotionEvent.obtainNoHistory to create a copy.

Note: Created flow keeps a strong reference to the View instance until the coroutine that launched the flow collector is cancelled.

Example of usage:

view.touch { event.action == MotionEvent.ACTION_DOWN }
.onEach { event ->
// handle touch event
}
.launchIn(uiScope)