How to programmatically scroll NestedScrollView within a CoordinatorLayout?

10,229

CollapsingToolbarLayout is collapsed base on AppBarLayout offseting tops and bottom of its children. You are offseting it properly with calling behavior nested pre scroll method. NestedScrollView needs to be scrolled by the same amount too:

        int targetScroll = mNestedScrollView.getScrollY() + 1000;
        mNestedScrollView.scrollTo(0,targetScroll);
        mNestedScrollView.setSmoothScrollingEnabled(true);
        ViewCompat.setNestedScrollingEnabled(mNestedScrollView, false);
        final int currentScrollY = mNestedScrollView.getScrollY();
        ViewCompat.postOnAnimationDelayed(mNestedScrollView, new Runnable() {
            int currentY = currentScrollY;
            @Override
            public void run() {
                if(currentScrollY == mNestedScrollView.getScrollY()){
                    ViewCompat.setNestedScrollingEnabled(mNestedScrollView, true);
                    return;
                }
                currentY = mNestedScrollView.getScrollY();
                ViewCompat.postOnAnimation(mNestedScrollView, this);
            }
        }, 10);
Share:
10,229
Alex Wih
Author by

Alex Wih

Updated on June 04, 2022

Comments

  • Alex Wih
    Alex Wih almost 2 years

    I have CoordinatorLayout and following items inside it:

    1. AppBarLayout with collapsable toolbar;
    2. NestedScrollView with some content

    I want to programmatically scroll my NestedScrollView up until Collapsable Toolbar is collapsed.

    I tried the code like this:

    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
    if (behavior != null) {
        behavior.onNestedPreScroll(coordinatorLayout, appBarLayout, nestedScrollView, 0, 1000, new int[2]);
    }
    

    But it just scrolls up and collapses the AppBar layout itself and NestedScrollView remains on its place.

    So,the question is how to scroll NestedScrollView up and made Collapsable Toolbar to collapse?

    I know the issue related somehow with Coordinator Layout's behavior, but I can't realize what is missed.

    Here is the exact layout:

    <android.support.design.widget.CoordinatorLayout
    android:id="@+id/event_coordinator_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
            <!-- some content -->
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/quick_return_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin">
    
            </android.support.v7.widget.Toolbar>
    
        </android.support.design.widget.CollapsingToolbarLayout>
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <!-- some content -->
    
    </android.support.v4.widget.NestedScrollView>