Android 带有 ViewGroup 布局的 CollapsingToolbarLayout 的视差效果不起作用

Android parallax effect CollapsingToolbarLayout with ViewGroup Layout is not working

我正在开发 Android 应用程序。在我的应用程序中,我使用 CollapsingToolbarLayout 来实现视差效果。在我使用 AppbarLayout、CollapsingToolbarLayout 和 ViewPager 之前,我工作得很好。但是这一次,我用 RelativeLayout 替换了内容的视图持有者。因此,当我从内容向上滚动时,工具栏不会折叠。我的意思是当我从下面屏幕截图中圈出的位置向上滚动时。

但是如果我从工具栏区域向上滚动,它就可以工作了。我指的是截图中的区域。

这是我的XML布局

<android.support.design.widget.CoordinatorLayout
    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:background="@android:color/background_light"
    android:fitsSystemWindows="true"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/ai_app_bar"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
        >

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

           <RelativeLayout
               android:background="@drawable/item"
               app:layout_collapseMode="parallax"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
              <View
                  android:background="@color/lightGray"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"></View>
           </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/ai_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin"
                />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


    <RelativeLayout
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:text="This is testing"
            android:textColor="@color/textColorPrimary"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/ai_review_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@android:drawable/btn_star"
        android:layout_margin="10dp"
        android:visibility="gone"
        app:layout_anchor="@id/ai_app_bar"
        app:layout_anchorGravity="bottom|right|end"
        />
</android.support.design.widget.CoordinatorLayout>

为什么从相关布局区域向上滚动时工具栏没有折叠?如果我从 RelativeLayout 区域向上滚动,如何解决工具栏折叠的问题?

您应该在 AppBarLayout 之外使用 RecyclerView 而不是 RelativeLayout 并将此视图标签添加到其中 app:layout_behavior="@string/appbar_scrolling_view_behavior"

string/appbar_scrolling_view_behavior maps to AppBarLayout.ScrollingViewBehavior, which is used to notify the AppBarLayout when scroll events occur on this particular view.

从这里开始Handling Scrolls with CoordinatorLayout

编辑

此外,CollapsingToolbar 需要 RecyclerView 或 NestedListView 才能工作。

在相对布局中,在相对布局中的每个标签关闭后关闭此标签

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fillViewport="true"
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_gravity="fill_vertical">