DrawerLayout + CollapsingToolbar + Fragments; Toolbar won't collapse or show image

江枫思渺然 提交于 2019-12-04 01:54:53

问题


As the title suggests, I have an activity that has a FrameLayout that houses my Fragments [I am NOT using a Tabs, just Transactions]. I also have a NavigationDrawer and Toolbar. All of these things work fine.

The issue is that I'm trying to add a CollapsingToolbarLayout to the Activity layout and have RecyclerView in the Fragment scroll and control the collapsing of the CollapsingToolbarLayout.

With the setup shown below, I'm able to use the NavigationDrawer, see the Toolbar and the expanded CollapsingToolbarLayout with NO image inside (odd issue there). I load the Fragment into the FrameLayout which contains a RecyclerView with 10 test views. This scrolls fine below the CollapsingToolbar but it does not collapse.

Activity Layout XML

<android.support.v4.widget.DrawerLayout
    android:id="@+id/dl_drawer_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:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/cl_dashboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

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

                <ImageView
                    android:id="@+id/iv_dashboard_header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:src="@mipmap/the_image"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="parallax"/>

                <include
                    android:id="@+id/toolbar"
                    layout="@layout/overlay_toolbar"
                    app:layout_collapseMode="pin"/>

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

        <FrameLayout
            android:id="@+id/fl_dashboard_fragmentframe"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

        </FrameLayout>

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nv_navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/overlay_drawer_header"
        app:menu="@menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>

Fragment Layout XML

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_fragment_dashboardhome"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

</android.support.v7.widget.RecyclerView>

I don't understand why this isn't working and I've run through a lot of documentation and examples with no luck. Any help or suggestion would be greatly appreciated.


回答1:


It turned out that the layout I was using for the Toolbar did not have the correct height value.

overlay_toolbar.xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    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="wrap_content"
    android:background="@color/get_blue"
    android:elevation="5dp"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/actionbar_getblue">

</android.support.v7.widget.Toolbar>

Adjustment to overlay_toolbar include call in Activity XML

<include
    android:id="@+id/toolbar"
    layout="@layout/overlay_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_collapseMode="pin"/>

After that single adjustment to the height the CollapsingToolbarLayout began working perfectly.




回答2:


app:layout_behavior should be applied to RecyclerView or any other View capable of nested scrolling such as NestedScrollView.

See this tutorial from codepath: https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

You can use app:layout_behavior="@string/appbar_scrolling_view_behavior" on RecyclerView you have in fragment.



来源:https://stackoverflow.com/questions/33132908/drawerlayout-collapsingtoolbar-fragments-toolbar-wont-collapse-or-show-ima

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!