Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GestureDetector cannot detect onScroll event with FrameLayout

I have a view that extends FrameLayout and need to be notified of the scrolling events on it. this view has an instance of a class that implements the GestureDetector which is invoked by the overriden onInterceptTouchEvent method.

    private class HorizontalScrollListener implements OnGestureListener {
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        ...
        return false;
    }
    @Override
    public boolean onDown(MotionEvent e) { 
         ...
         return false; 
    }
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
        return false;
    }
    @Override
    public void onLongPress(MotionEvent e) {
        ...
        System.out.println();
    }
    @Override
    public void onShowPress(MotionEvent e) {}
    @Override
    public boolean onSingleTapUp(MotionEvent e) { return false; }
}

The only problem is that the onDown and onLongPress methods could get called wheen I try to scroll but the actual onScroll methods never gets invoked.

    @Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    boolean result = super.onInterceptTouchEvent(event);

    if (gestureDetector.onTouchEvent(event)) {
        return result;
    } else {
        return false;
    }       
}
like image 786
Mahorad Avatar asked Nov 13 '22 10:11

Mahorad


1 Answers

onInterceptTouchEvent is not called again for a motion sequence once it returns true. Events are sent to onTouchEvent directly afterwards (since they are now being intercepted from the children).

You need two changes here:

  • OnGestureListener.onDown() should return true so the detector can process more complex gestures like scrolls
  • onInterceptTouchEvent should always return false to keep the stream of events flowing to this method
like image 112
Tugrul Ates Avatar answered Nov 16 '22 02:11

Tugrul Ates