0

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)?

ScrapeW
  • 489
  • 8
  • 16
  • https://stackoverflow.com/questions/3756383/what-is-meaning-of-boolean-value-returned-from-an-event-handling-method-in-andro – ADM Sep 09 '20 at 17:14
  • @ADM Interesting is like I thought, but what I still don't understand is why if I drag the item the OnClick listener doesn't fired, cause I'm returning false And it should move forward to the next listener (OnClick) – ScrapeW Sep 09 '20 at 17:33
  • 1
    Onclick event captured when `ACTION_DOWN` followed by `ACTION_UP` within a small time frame greater then that it will be treated as long click.. That's how these listeners are implemented. – ADM Sep 09 '20 at 17:37

0 Answers0