使用具有基于片段元素的布局的折叠工具栏

Using a collapsing toolbar with a fragment-element-based layout

我有一个自定义主详细信息流程,我希望它具有您在默认模板中找到的折叠工具栏效果。我不明白如何使用嵌套。

我的 activity 有两个变体:

masterdetail 流程

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    tools:context="com.example.android.sunshine.app.MainActivity">

    <!--
    This layout is a two-pane layout for the Items master/detail flow.
    -->

    <fragment
        android:id="@+id/fragment_main"
        android:name="xyz.selfenrichment.robertotomas.three_two_one_rotate.MainFragment"
        android:tag="@string/tag_gridview_one_column"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        tools:context="xyz.selfenrichment.robertotomas.three_two_one_rotate.MainFragment"
        tools:layout="@layout/fragment_main" />

    <FrameLayout
        android:id="@+id/fragmentcontainer_detail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        tools:layout="@layout/fragment_detail" />

</LinearLayout>

单窗格

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_main"
    android:name="xyz.selfenrichment.robertotomas.three_two_one_rotate.MainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:tag="@string/tag_gridview_two_columns"
    tools:context="xyz.selfenrichment.robertotomas.three_two_one_rotate.MainFragment"
    tools:layout="@android:layout/list_content" />

我认为我的折叠工具栏应该是这样的:

<android.support.design.widget.CoordinatorLayout ...
...
    <android.support.design.widget.AppBarLayout ...
    ...
        <android.support.design.widget.CollapsingToolbarLayout ...
        ...
            <android.support.v7.widget.Toolbar

现在,我必须将两者结合起来并添加布局行为,例如:

主从流程

<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:fitsSystemWindows="true"
    tools:context="xyz.selfenrichment.robertotomas.popularmovies.PosterBoardActivity">

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

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

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

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:divider="?android:attr/dividerHorizontal"
        android:orientation="horizontal"
        tools:context="com.example.android.sunshine.app.MainActivity"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!--
        This layout is a two-pane layout for the Items master/detail flow.
        -->

        <fragment
            android:id="@+id/fragment_main"
            android:name="xyz.selfenrichment.robertotomas.popularmovies.MainFragment"
            android:tag="@string/tag_gridview_one_column"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3"
            tools:context="xyz.selfenrichment.robertotomas.popularmovies.MainFragment"
            tools:layout="@layout/fragment_main" />

        <FrameLayout
            android:id="@+id/fragmentcontainer_detail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            tools:layout="@layout/fragment_detail" />

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_menu_manage"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end" />

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

但是,如果我将布局行为添加到 fragment 标记,或添加到基础片段 xml(或两者):

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:stretchMode="columnWidth"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

无论哪种方式,滚动 gridview 都不会折叠工具栏。

编辑:这特别令人困惑,因为我看到一个非常相似的示例 显然确实有效。

哇哦 :) 我找到了。我设置了 layout_behavior,但是在 class 片段中,我将它附加到数据适配器,我放置了这些行:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mGridView.setNestedScrollingEnabled(true);
    }