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

时间:2016-04-06 23:43:42

标签: android-layout master-detail coordinator-layout

我有一个自定义主详细信息流,我希望在默认模板中找到折叠工具栏效果。我不了解嵌套以使其发挥作用。

我的活动有两种变体:

masterdetail flow

<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" />

无论哪种方式无关紧要,滚动网格视图不会折叠工具栏。

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

1 个答案:

答案 0 :(得分:1)

呜啊:)我找到了。我已经设置了layout_behavior,但是在片段类中,我将它附加到数据适配器上,我将这些行放在:

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