Android ListView onTouchEvent does not always give ACTION_DOWN

My problem is that when I try to drag an item into my ListView, I do not always get the ACTION_DOWN event. I received many ACTION_MOVE events and only one ACTION_UP event. It is not always so. I got ACTION_DOWN 3 times. It confused me.

I looked at similar questions, but the answers do not seem to suit me. Can anyone think why this is happening?

thanks

//list_client -- a listview list_client.setOnTouchListener(new View.OnTouchListener() { float f1 = -1, f2 = -1 ; @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_DOWN: f1 = event.getRawY(); break; case MotionEvent.ACTION_MOVE: f2 = event.getRawY(); if(f2 - f1 > 50){ if(View.VISIBLE != rl_search_and_add.getVisbility() && ){ rl_search_and_add.setVisibility(View.VISIBLE); } f1 = f2; }else if (f2 - f1 < -50){ rl_search_and_add.setVisibility(View.GONE); f1 = f2; } break; case MotionEvent.ACTION_UP: f1 = -1; f2 = -1; break; } return false; } }); 
+4
source share
2 answers

This behavior may be several.

  • It is possible that the user skipped the list a little, and MotionEvent.ACTION_DOWN was handled by another component, but as soon as the user continues to drag and drop into the list view area, you got MotionEvent.ACTION_MOVE actions.
  • Another possibility is that you had a lot of events, and they were transferred to a historical event, and you received only the last event. You can use all the historical MotionEvent methods to see them.
+1
source

you should override onInterceptTouchEvent as follows:

 @Override public boolean onInterceptTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: yDown = event.getRawY(); break; default: break; } return super.onInterceptTouchEvent(event); } 

then using yDown in your program

0
source

Source: https://habr.com/ru/post/1494447/


All Articles