Search code examples
androidontouchlistenerandroid-scrollview

Scrollview not flinging after onTouchListener attached


I have a ScrollView which has a ViewTreeObserver.OnScrollChangedListener which is not the problem, but once i set the OnTouchListener to figure out when the ScrollView has stopped scrolling, the flinging action on the scrollview does not happen anymore. here is my code:

if i do this it doesnt actually run the method that i have for the onTouch Event and the flinging still happens:

@Override
    public boolean onTouch(View view, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            horizontalScrollClipping((ScrollView)view, view.getScrollY());
            return false;  //returning false instead of true
        }
        return false;
    }

if i do this, the method inside the onTouch Event fires off, but the flinging for the scrollview does not happen :

@Override
    public boolean onTouch(View view, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            horizontalScrollClipping((ScrollView)view, view.getScrollY());
            return true;  //returning true instead false
        }
        return false;
    }

And then finally if i do this, the scrolling on the ScrollView gets completely disabled:

@Override
    public boolean onTouch(View view, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            horizontalScrollClipping((ScrollView)view, view.getScrollY());
            return true;   //return true for both
        }
        return true;
    }

here is the horizontalScrollClipping method that is shown in the Touch Event:

private void horizontalScrollClipping(ScrollView scrollView, int scrollY) {
        if (scrollY > (image.getHeight() * 0.60)) {
            scrollView.smoothScrollTo(0, image.getHeight());
        }
    }

just wondering if anyone could clue me in on why the default flinging action for the scrollview gets disabled when the OnTouchListener is attached and returning true, and as well why the method i have inside the OnTouch event does not fire off when im returning false. Thanks in advance, any help is much appreciated.

EDIT: So instead of checking if the user is done scrolling during the onTouch interface i just calculated if the user was done scrolling in the OnScrollChangedListener like so:

scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                int scrollY = scrollView.getScrollY();
                if ((scrollY - lastScrollY) < 5 && (scrollY - lastScrollY) > 0) {
                    horizontalScrollClipping((ScrollView)scrollView, scrollY);
                }
                lastScrollY = scrollY;
            }
        });

Where lastScrollY is a variable initialized at the top of my class set to 0.


Solution

  • If you return true from onTouch you're stating that you have handled the event and that no other listeners should attempt to handle it. If I had to GUESS ScrollView is also a listener to that onTouch method and when you return true the ScrollView's onTouch is never called because your method handled the event.