64

I am trying to follow the Google Docs on using the CoordinatorLayout but i am having an issue with the ScrollView inside the CoordinatorLayout. Basically, the Toolbar normally would collapse with a RecyclerView or a Listview when scrolling down. Now with a ScrollView it will not collapse.

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

        <TextView
            android:id="@+id/tv_View"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/filler"
            style="@style/TextAppearance.AppCompat.Large"
            />

    </ScrollView>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            />

    </android.support.design.widget.AppBarLayout>

    </android.support.design.widget.CoordinatorLayout>
AmaJayJB
  • 1,383
  • 2
  • 13
  • 19

5 Answers5

153

The ScrollView does not cooperate with the CoordinatorLayout. You have to use NestedScrollView instead of ScrollView

TheoK
  • 3,138
  • 5
  • 22
  • 33
  • 8
    This actually does answer the question, the toolbar won't collapse because `CoordinatorLayout` doesn't support `ScrollView`, OP can get the requested behavior by switching from `ScrollView` to `NestedScrollView`. – marmor Jun 11 '15 at 11:31
  • @dbugger Why doesn't this answer provide an answer? – Gabriele Mariotti Jun 17 '15 at 07:56
  • I spent over an hour trying to figure out why it wasn't scrolling properly. Thanks! – Flyview Mar 08 '16 at 19:46
  • 2
    confirmed working - even in nested Fragments which are replaced via supportFragmentManager transaction – kosiara - Bartosz Kosarzycki Mar 17 '16 at 10:55
  • Thanks a lot for this! NestedScrollView also works correctly with the CoordinatorLayout when it's inside a viewpager – danwilkie Dec 30 '16 at 16:46
  • Whenever you see something like this, go directly to Android bug tracker and report a bug. Post it back here, and we'll vote it up. Thousands of developers found this answer (workaround) useful, and yet no issues were reported. – milosmns Sep 27 '19 at 13:53
36

Use NestedScrollView to collapse your scrollview as a child of Coordinator Layout. Replace your code with this code:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <TextView
            android:id="@+id/tv_View"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/filler"
            style="@style/TextAppearance.AppCompat.Large"
            />

    </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            />

    </android.support.design.widget.AppBarLayout>

    </android.support.design.widget.CoordinatorLayout>
Anupriya
  • 1,473
  • 2
  • 14
  • 23
11

You can keep the ScrollView and add this XML property : android:nestedScrollingEnabled="true" so it knows the CoordinatorLayout as a sibling and keep in mind this property is supported just in lollipop version and above.

2

Use a NestedScrollView instead of a regular ScrollView when using CoordinatorLayout.

To make the CollapsingToolbarLayout scroll you can trigger the scroll behavior by setting a minimum height of the child Layout of the NestedScrollView to *1000dp.

android:minHeight="1000dp"

Layout:

<android.support.v4.widget.NestedScrollView
app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!--to trigger scroll behavior-->
    <LinearLayout android:minHeight="1000dp"/>

</android.support.v4.widget.NestedScrollView>

*SupportDesignDemos example here: https://github.com/android/platform_development/blob/master/samples/SupportDesignDemos/res/layout/include_appbar_scrollview.xml

TouchBoarder
  • 6,285
  • 2
  • 49
  • 60
1

The actual answer should be that CoordinatorLayout doesn't work with ScrollView, because ScrollView is not implementing NestedScrollingChild interface. NestedScrollView is a ScrollView with NestedScrollingChild implementation. If you want to learn more about nested scrolling I made a blog post about it.

  • Please post the relevant code from the blog post and how it solves OP's question. – Frakcool Oct 19 '15 at 15:37
  • I just wanted to point out the root cause of the problem. The accepted solution is ok, but doesn't provide the explanation why changing ScrollView with NestedScrollView fixes the issue. – Grzegorz Gajewski Oct 19 '15 at 15:57
  • 2
    I'm just saying, your solution might be good, it just needs some code from your part, imagine your blog gets deleted (by whatever reason) then, it might become useless. I'm not that into Android development but I know more or less how Stack Overflow works... Not 100% necessary but if you add code to your answer (this and future ones), your Q&A will be much better. It's just a recommendation that if you follow it up I'll be glad to upvote – Frakcool Oct 19 '15 at 16:13