【问题标题】:DrawerLayout + CollapsingToolbar + Fragments; Toolbar won't collapse or show imageDrawerLayout + CollapsingToolbar + Fragments;工具栏不会折叠或显示图像
【发布时间】:2015-10-14 18:27:04
【问题描述】:

正如标题所示,我有一个活动,其中有一个 FrameLayout,其中包含我的 Fragments [我没有使用选项卡,只使用事务]。我也有NavigationDrawerToolbar。所有这些都可以正常工作。

问题是我正在尝试将CollapsingToolbarLayout 添加到Activity 布局中,并在Fragment 滚动中添加RecyclerView 并控制CollapsingToolbarLayout 的折叠。

通过下面显示的设置,我可以使用NavigationDrawer,查看Toolbar 和扩展的CollapsingToolbarLayout,里面没有图像(奇怪的问题)。我将Fragment 加载到FrameLayout 中,其中包含带有10 个测试视图的RecyclerView。它可以在 CollapsingToolbar 下方正常滚动,但不会折叠。

活动布局 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>

片段布局 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>

我不明白为什么这不起作用,并且我已经浏览了很多文档和示例,但没有运气。任何帮助或建议将不胜感激。

【问题讨论】:

    标签: android android-fragments android-collapsingtoolbarlayout


    【解决方案1】:

    原来我用于Toolbar 的布局没有正确的高度值。

    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>
    

    对overlay_toolbar的调整包括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"/>
    

    在对高度进行单次调整后,CollapsingToolbarLayout 开始完美运行。

    【讨论】:

      【解决方案2】:

      app:layout_behavior 应应用于 RecyclerView 或任何其他能够嵌套滚动的 View,例如 NestedScrollView。

      从代码路径查看本教程:https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

      您可以在片段中的 RecyclerView 上使用 app:layout_behavior="@string/appbar_scrolling_view_behavior"。

      【讨论】:

      • 我从 Activity 的 FrameLayout 中删除了 app:="@string/appbar_scrolling_view_behavior" 并将其添加到 Fragment 的 RecyclerLayout 中,但没有成功。然后我尝试将 app:="@string/appbar_scrolling_view_behavior" 留在 Activity 的 FrameLayout 中,并将其添加到 Fragment RecyclerView。不幸的是,这两者都导致了相同的非滚动行为。
      • android-developers.blogspot.com/2015/05/… 说:app:layout_collapseMode="pin" 以确保工具栏本身在视图折叠时保持固定在屏幕顶部。不确定这是否有问题。您可以查看guides.codepath.com/android/…中的“创建视差动画”部分@
      • 我发现了这个问题,感谢您的意见,但我已经使用了所有这些资源并且修复是由于工具栏高度值。
      猜你喜欢
      • 2019-08-05
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多