This question is a follow up to this question - How to handle both OnTouchListener event and OnClickListener event
I tried to implement what the accepted answer suggested:
holder.itemView.container.setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_UP -> {
holder.triggerListner()
}
MotionEvent.ACTION_MOVE -> {
//start dragging the view
}
}
return@setOnTouchListener true
}
That does solve my problem, but the drag detection was bad.
I missed 2/3 drags out of 5(tried to drag and nothing happen), while if I detect the drag event in MotionEvent.ACTION_DOWN
it worked perfect but it didn't detect the onClick listener..
So I played a little with the code and I went back to MotionEvent.ACTION_DOWN
detection, but this time instead of returning return@setOnTouchListener true
I returned false
holder.itemView.container.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
//start dragging
}
return@setOnTouchListener false
}
And it is working perfectly now, Detect when I drag, if I'm not dragging it call the click listener inside the holder.
So I haven't problem with the code, I just want to understand why this is working.
I read the docs and they said there that returning true/false from the touch listener if the event is consumed or not.
So if I'm returning false I can "move forward" to other events(the accpeted answer here also mentioned that - Is it possible to use dragging events and click event on the same Textview?).
According to this I understand why if I am clicking on item it detect the drag event(that doesn't do anything cause I'm not dragging the view) and then fire the onClickListener, but what I am not understanding is why if I drag the item and swap position with other item, the OnClick listener doesn't fired(I don't want it to be fired, but I really want to understand why this is happening)?