Search code examples
androidandroid-scrollview

Android SetNestedScrollingEnabled not called?


I have a nested child scrollview that is set to disable scrolling until the parent scrollview completes scrolling up. Setting the childScrollView.setNestedScrollingEnabled(false) works initially, and is reenabled childScrollView.setNestedScrollingEnabled(true) after the parent is scrolled up completely. However, while this is working, I want to again disable the nested scrollview after the parent is scrolled to the bottom, but it's not working. Any ideas?

MainActivity (snippet) :

ResponsiveScrollView parentScroll = (ResponsiveScrollView) findViewById(R.id.parentScroll);

final ScrollView childScroll = (ScrollView) findViewById(R.id.childScroll);

childScroll.setNestedScrollingEnabled(false); // disabled by default

parentScroll.setOnEndScrollListener(new PriorityNestedScrollView.OnEndScrollListener() {

    @Override
    public void onUpEndScroll() {
        Log.i(TAG, "scrolling up has ended"); // successfully fires

        childScroll.setNestedScrollingEnabled(true); // working...
    }

    @Override
    public void onDownEndScroll() {
        Log.i(TAG, "scrolling down has ended"); // successfully fires

        childScroll.setNestedScrollingEnabled(false); // not working...
    }
});

ResponsiveScrollView (snippet) :

@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
    super.onScrollChanged(x, y, oldX, oldY);

    // scroll up ended
    if (y >= END) {
        if (onEndScrollListener != null) {
            onEndScrollListener.onUpEndScroll();
        }
    }

    // scroll down ended
    if (y <= START) {
        if (onEndScrollListener != null) {
            onEndScrollListener.onDownEndScroll();
        }
    }
}

Solution

  • Here's how I accomplished this. I intercept the nested scroll touch events until the parent has reached the top.

    ResponsiveScrollView :

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (isEnableScrolling()) {
            return super.onInterceptTouchEvent(ev);
        } else {
            return true;
        }
    }
    
    ...
    
    boolean enableScrolling = false // disabled by default
    
    public boolean isEnableScrolling() {
        return enableScrolling;
    }
    
    public void setEnableScrolling(boolean enableScrolling) {
        this.enableScrolling = enableScrolling;
    }
    

    MainActivity :

    @Override
    public void onUpEndScroll() {
        Log.i(TAG, "scrolling up has ended");
    
        parentScroll.setEnableScrolling(true); // enable scrolling
    }
    
    @Override
    public void onDownEndScroll() {
        Log.i(TAG, "scrolling down has ended");
    
        parentScroll.setEnableScrolling(false); // disable scrolling
    }