0

I am using a ScrollView, that has a scroll trigger, in unscrollable state, I would like to detect the possible scroll, but instead of scrolling the view i would like to do other things with this value.

@Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (!mScrollable) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    y = event.getY();

                case MotionEvent.ACTION_UP:
                    y = event.getY();

                case MotionEvent.ACTION_MOVE:
                    double newY = event.getY();
                    double difY = y - newY;
                    mCallbacks.onScrollChanged((int) difY, false, false);
                    y = newY;

            }
            return false;
        } else return super.onInterceptTouchEvent(event);
    }

This code return difY as 0, if the cases return true, than ACTION_MOVE is not called at all.

Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48

1 Answers1

1

onInterceptTouchEvent is used to tell the Android Framework that your View is interested on that currently happening touch event and would like to keep receiving updates about it, until it's over.

To actually process the touch you should override onTouchEvent, that's the callback that will keep receiving the touch until it is cancelled or ACTION_UP.

A quick/dirty workaround it is to return always true on onInterceptTouchEvent and move your current code to onTouchEvent. The upside of this way is that it's easy to do, the downside is that it will block all your view parentView from receiving touch events.

The proper way of doing it is returning true onInterceptTouchEvent only after your view detects a difY != 0 and then keep processing the touch during onTouchEvent

Budius
  • 39,391
  • 16
  • 102
  • 144