1

我有一个自定义主详细信息流,我希望它具有像您在默认模板中找到的折叠工具栏效果。我不理解嵌套让它工作。

我的活动有两种变体:

主从流程

<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 不会折叠工具栏。

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

4

1 回答 1

2

呜呼:)我找到了。我设置了 layout_behavior,但是在我将其附加到数据适配器的片段类中,我放置了以下几行:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mGridView.setNestedScrollingEnabled(true);
    }
于 2016-04-07T21:03:20.420 回答