2

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>

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
Alex Wih
  • 1,015
  • 2
  • 11
  • 24

1 Answers1

2

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);
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • I'm trying to scroll to a view inside nested scroll view but it's not working – Edijae Crusar Jun 06 '18 at 17:00
  • @EdijaeCrusar were you able to do it? me too, i need to scroll to a view inside a nested scroll view. can you pls share your solution here: https://stackoverflow.com/questions/52083678/nestedscrollviews-smoothscrollto-behaves-exactly-like-smoothscrollby – user1506104 Sep 04 '18 at 04:01
  • @user1506104 Yes. see my answer here: [https://stackoverflow.com/a/52166191/3671509](https://stackoverflow.com/a/52166191/3671509) – Edijae Crusar Sep 04 '18 at 12:20