Android 折叠工具栏和recyclerview在滚动时不会折叠

Android 折叠工具栏和recyclerview在滚动时不会折叠,android,android-recyclerview,scrollview,android-toolbar,Android,Android Recyclerview,Scrollview,Android Toolbar,我在我的一个活动中实现了折叠工具栏,滚动时工具栏不会折叠: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipeContainer" android:layout_width="

我在我的一个活动中实现了折叠工具栏,滚动时工具栏不会折叠:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:fitsSystemWindows="true"
        android:layout_height="@dimen/app_bar_height"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:scrollbars="none" />


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


我还向gradle添加了所需的库。向下滚动时,工具栏保持在同一位置。知道为什么会发生这种情况吗?

AppBarLayout需要协调布局

此视图在很大程度上取决于是否被用作CoordinatorLayout中的直接子级。如果在不同的视图组中使用AppBarLayout,它的大部分功能将无法工作

引自developer.android.com/reference/android/support/design/widget/AppBarLayout.html

还可以查看以下教程:

此代码可能对您有所帮助。在“活动”中定义折叠工具栏并编写 展开和折叠的方法



我在里面有另一个布局recyclerview@RainMan:在这种情况下,您不需要线性布局。CoordinatorLayout将根据视图的行为相应地定位视图。移除
线性布局
,在
回收视图
周围移动
SwipeRefreshLayout
,然后使顶层a
协调布局
@DeeV,谢谢您的评论,我采用了与您描述的相同的方法,但是现在,当recycleview的内容位于工具栏顶部时。@RainMan:您可以将布局行为添加到recycler视图中吗。app:layout_behavior=“@string/appbar_scrolling_view_behavior”,这应该可以解决问题。
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_car_tracker"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.RecyclerView
       android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.user.Activity" />

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/toolbarbackgroundcolor"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/toolbarbackgroundcolor"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:foregroundGravity="center"
                    android:src="@drawable/location"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax"/>

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



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