In normal view, we can have onTouchEvent
override fun onTouchEvent(event: MotionEvent?): Boolean {
when (event?.action) {
MotionEvent.ACTION_DOWN -> {}
MotionEvent.ACTION_MOVE -> {}
MotionEvent.ACTION_UP -> {}
else -> return false
}
invalidate()
return true
}
In Jetpack Compose, I can only find we have the tapGestureFilter
in the modifier, which only takes the action from the ACTION_UP
only.
Modifier
.tapGestureFilter { Log.d("Track", "Tap ${it.x} | ${it.y}") }
.doubleTapGestureFilter { Log.d("Track", "DoubleTap ${it.x} | ${it.y}") }
Is there an equivalent onTouchEvent
for Jetpack Compose?
We have a separate package for that, which is pretty useful. There are two main extension functions that would be suitable for you:
pointerInput
- docspointerInteropFilter
- docsIf you want to handle and process the event I recommend using
pointerInteropFilter
which is the analogue ofView.onTouchEvent
. It's used along withmodifier
:That will be Compose adjusted code to your specified
View.onTouchEvent
sample.P.S. Don't forget about
@ExperimentalPointerInput
annotation.