Scrollview doesn't appear when onTouchListener is on

I have a ScrollView that has a ViewTreeObserver.OnScrollChangedListener, which is not a problem, but once I set the OnTouchListener to figure out when the ScrollView has stopped scrolling, the discarding action in the scrollview no longer occurs. here is my code:

if I do this, it doesn't actually fire the method 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 is disabled, but the jump to scroll 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;
    }

      

Finally, if I do this, the scrolling in the ScrollView turns off completely:

@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 the horizontalScrollClipping method is used, which 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 can understand me why the default action for the scrollview is disabled when the OnTouchListener is connected and returns true, and also why the method I have inside the OnTouch event doesn't fire when im returned is false. Thanks in advance, any help is greatly appreciated.

EDIT: So instead of checking the user's scrolling during the onTouch interface, I just figured out if the user was scrolled 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 the variable initialized at the top of my class, set to 0.

+3


source to share


1 answer


If you return true from onTouch

, you are declaring that you have handled this event and that no other listeners should try to handle it. If I needed GUESS is ScrollView

also a listener to this method onTouch

, and when you return true, the ScrollView onTouch is never called because your method is handling the event.



+3


source







All Articles