使用 ScrollView(和 NestedScrollView)时 CollapsingToolbarLayout 不折叠

CollapsingToolbarLayout not collapsing when using ScrollView (and NestedScrollView)

我正在尝试将 CollapsingToolbarLayout 与 ScrollView 一起使用,但我没有让它工作。我试试这个:

<android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_below="@+id/toolbar"
                android:scrollbars="none"
                android:overScrollMode="never"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:behavior_overlapTop="192dp"
                android:layout_height="match_parent">

            <LinearLayout android:layout_width="match_parent"
                          android:orientation="vertical"
                          android:padding="@dimen/medium_padding"
                          android:layout_height="match_parent">

                          <!-- my content -->


            </LinearLayout>

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



        <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.v7.widget.Toolbar>
        <ImageView
                android:src="@drawable/fondo_drawer"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                app:layout_collapseMode="parallax"
                android:minHeight="100dp"/>

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

我用 ScrollView 和 NestedScrollView 测试失败。

有什么想法吗?提前致谢!

我觉得

android:fitsSystemWindows="true" is the cause.

查看我的简单工作示例代码。

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/product_detail_main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:apptools="http://schemas.android.com/tools"
    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="@dimen/product_detail_appBar_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/detail_product_collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:foregroundGravity="bottom|right"
            android:foregroundTintMode="add"
            android:clipToPadding="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="@dimen/space_xxlarge"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/image"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/product_toolBar_title"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="pin"
              app:navigationIcon="@drawable/ic_arrow_back_white_24dp"
              app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
           app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

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

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

<android.support.v4.widget.NestedScrollView
 ........
<android.support.design.widget.CoordinatorLayout

你可以看看这个linkhttp://inthecheesefactory.com/blog/android-design-support-library-codelab/en

我运行陷入同样的​​错误abhishek's答案很完美

我想补充一点。如果你只为 AppBarLayout 做 android:fitsSystemWindows="true" 它会起作用,你不必为 ImageViewToolBar 设置它 - 因为它们是 AppBarLayout 的一部分,这将是也适用于他们。

看看这里 - https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec#.an1hvz44l

System windows are the parts of the screen where the system is drawing either non-interactive (in the case of the status bar) or interactive (in the case of the navigation bar) content.

Most of the time, your app won’t need to draw under the status bar or the navigation bar, but if you do: you need to make sure interactive elements (like buttons) aren’t hidden underneath them. That’s what the default behavior of the android:fitsSystemWindows=“true” attribute gives you: it sets the padding of the View to ensure the contents don’t overlay the system windows.