Android - 协调布局、抽屉式布局和片段

8

我花了一些时间试图实现这个功能,并进行了充分的研究,但是无法使其工作。 在Chris Banes的cheesesquare示例中,当ViewPager滚动时,他使得工具栏消失。 ViewPager直接包含在抽屉布局中,在NavigationView之前。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

    <!-- THIS CONTAINS COORDINATOR LAYOUT with APPBAR LAYOUT + VIEWPAGER -->
    <include layout="@layout/include_list_viewpager"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view"/>

</android.support.v4.widget.DrawerLayout>

include_list_viewpager文件是这样的:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <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"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_done"/>

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

我希望使用FrameLayout(或NestedScrollView)代替ViewPager。这样,当用户点击抽屉布局中的项目时,我就可以动态加载片段,并且它们都会有一个很好的动画工具栏。到目前为止,我无法在调用的片段中使工具栏滚动消失。我想知道这是否可能。
是否有任何人做到了这一点?非常感谢您提供任何指针。

展示您的布局,包括列表视图翻页器。 - Dhaval Parmar
从Cheesesquare应用程序中的include_list_viewpager内容进行编辑。 - ticofab
1个回答

7
<android.support.design.widget.CoordinatorLayout
    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.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/mToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

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

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.view.ViewPager
        android:id="@+id/tab_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

        <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"/>
    </android.support.v4.widget.DrawerLayout>

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

CoordinatorLayout必须是所有视图的父级。 DrawerLayout获得layout_behavior。 由于您想与viewpager的元素交互,因此它需要位于DrawerLayout的顶层视图中。


协调布局必须是所有视图的父级。抽屉式布局获取layout_behavior属性。如果您希望与viewpager的元素进行交互,则它应该位于抽屉式布局的顶部视图中。

对我来说,这段代码不起作用,我已经添加了缺失的CoordinatorLayout标签,但是我的AppBarLayout内的视图仍然没有任何反应,一直保持稳定。有什么想法吗? - cV2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接