Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MotionEvent.ACTION_DOWN in Android is too sensitive. This event is received even if the screen is simply touched for a moment

I have a layout(A table layout). Whenever the user performs a "down" gesture, a specific function needs to be called. However,the ontouchlistener captures the event immediately instead of waiting until the user performs a proper "pull down" gesture.

My code is :

homePageTableLayout.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                if ((event.getAction() == MotionEvent.ACTION_DOWN)) {
                    startSyncData();
                }
                return true;
            }
        });

Currently, what's happening is that the startSyncData() function gets called immediately. The end result should be somewhat similar to the commonly used "pulltorefresh" library for android but in this usecase, I don't have to update the view. I just have to post some data to the server. I can't use the "pulltorefresh" library because it does not support "pulling" a tablelayout. I tried putting the tablelayout inside a scrollview but that only spoiled the UI.

like image 427
Umang Mathur Avatar asked Apr 22 '15 08:04

Umang Mathur


People also ask

Which method you should override to control your touch action in Android?

1.1. You can react to touch events in your custom views and your activities. Android supports multiple pointers, e.g. fingers which are interacting with the screen. The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method. you override the onTouchEvent() method.

What is Action_down?

ACTION_DOWN is for the first finger that touches the screen. This starts the gesture. The pointer data for this finger is always at index 0 in the MotionEvent. ACTION_POINTER_DOWN is for extra fingers that enter the screen beyond the first. The pointer data for this finger is at the index returned by getActionIndex().

What is MotionEvent in Java?

android.view.MotionEvent. Object used to report movement (mouse, pen, finger, trackball) events. Motion events may hold either absolute or relative movements and other data, depending on the type of device.


2 Answers

You can try doing it in ACTION_MOVE or ACTION_UP - if you need it to be done exclusively on swipe down gesture, this should help, introduce four global floats: x, y, x1, y1, then in ACTION_DOWN:

x = event.getX();
y = event.getY();

ACTION_MOVE or ACTION_UP:

x1 = event.getX();
y1 = event.getY();

and then in ACTION_UP:

if (y1 > y) {        //pointer moved down (y = 0 is at the top of the screen)
    startSyncData(); 
}
like image 175
Klotor Avatar answered Oct 13 '22 00:10

Klotor


Its not that easy. It works properly since you are catching MotionEvent.ACTION_DOWN. It doesn't mean a gesture like swipe down it means that user touched the screen (finger down, the touch or gesture just started). Now you need to catch MotionEvent.ACTION_UP (finger up, gesture or touch ends here) and decide if there was a gesture you need. http://developer.android.com/training/gestures/detector.html
http://developer.android.com/training/gestures/index.html

like image 21
Stan Avatar answered Oct 12 '22 23:10

Stan