CoordinatorLayout并隐藏工具栏

时间:2015-08-21 13:45:44

标签: android android-layout android-fragments

我正在使用单个活动 - 多个片段结构。在活动中我使用v7工具栏作为操作栏。我有使用viewpage的服务器片段,还有一些带有标签的片段。在我的一个片段中,我有recyclerview。我的目标是当我滚动时我想折叠栏并隐藏浮动动作按钮。我的浮动按钮位于CoordinatorLayout内部,因此它可以正确显示。但这是在片段的布局内(是viewpager的一部分,而activity_main.xml的根元素是另一个CoordinatorLayout。片段CL是否拦截活动布局中CL的工作,因为当我滚动回收器时没有任何反应。

我应该为每个片段使用新的工具栏,或者我可以使用一个 活动并实现不同的行为成片段?
任何人都可以参考任何使用CoordinatorLayout和的例子 在嵌套片段中折叠条形码?

activity_main.xml中

    <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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways" />
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" />

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

1 个答案:

答案 0 :(得分:2)

CoordinatorLayout的工作没有被拦截。将app:layout_behavior添加到作为片段容器的框架布局中完成了这项工作。 现在看起来像这样:

    <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity" />
相关问题