Search code examples
androidgestureontouchlistener

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.


Solution

  • 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(); 
    }