1 METHOD:
I figured out that the best method to do this is detecting the first touch saving the points x and y and then confront it with the second touch. If the distance between the first click and the second one is quite close (I put 10% as an approximation) then the touch a simple click otherwise is a scrolling movement.
/**
* determine whether two numbers are "approximately equal" by seeing if they
* are within a certain "tolerance percentage," with `tolerancePercentage` given
* as a percentage (such as 10.0 meaning "10%").
*
* @param tolerancePercentage 1 = 1%, 2.5 = 2.5%, etc.
*/
fun approximatelyEqual(desiredValue: Float, actualValue: Float, tolerancePercentage: Float): Boolean {
val diff = Math.abs(desiredValue - actualValue) // 1000 - 950 = 50
val tolerance = tolerancePercentage / 100 * desiredValue // 20/100*1000 = 200
return diff < tolerance // 50<200 = true
}
var xPoint = 0f
var yPoint = 0f
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
when(event.action) {
MotionEvent.ACTION_DOWN -> {
xPoint = event.x
yPoint = event.y
return true
}
MotionEvent.ACTION_UP -> {
if (!approximatelyEqual(xPoint, event.x, 10f) || !approximatelyEqual(yPoint, event.y, 10f)) {
//scrolling
} else {
//simple click
}
}
}
return false
}
2 METHOD:
Another way to do the same thing is by using the GestureDetector class:
interface GestureInterface {
fun setOnScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float)
fun onClick(e: MotionEvent)
}
class MyGestureDetector(val gestureInterfacePar: GestureInterface) : SimpleOnGestureListener() {
override fun onSingleTapUp(e: MotionEvent): Boolean {
gestureInterfacePar.onClick(e)
return false
}
override fun onLongPress(e: MotionEvent) {}
override fun onDoubleTap(e: MotionEvent): Boolean {
return false
}
override fun onDoubleTapEvent(e: MotionEvent): Boolean {
return false
}
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
return false
}
override fun onShowPress(e: MotionEvent) {
}
override fun onDown(e: MotionEvent): Boolean {
return true
}
override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float): Boolean {
gestureInterfacePar.setOnScroll(e1, e2, distanceX, distanceY)
return false
}
override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
return super.onFling(e1, e2, velocityX, velocityY)
}
}
and finally, bind it with your view:
val mGestureDetector = GestureDetector(context, MyGestureDetector(object : GestureInterface {
override fun setOnScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float) {
//handle the scroll
}
override fun onClick(e: MotionEvent) {
//handle the single click
}
}))
view.setOnTouchListener(OnTouchListener { v, event -> mGestureDetector.onTouchEvent(event) })