Android - Coordinator Layout、Drawer Layout 和 Fragments

标签 android android-toolbar android-design-library android-coordinatorlayout

我花了一些时间来尝试实现它,并做了一些研究,但无法让它发挥作用。 在cheesesquare Chris Banes 的例子,当滚动 ViewPager 时,他使工具栏滚动。 ViewPager 直接包含在他的抽屉布局中,就在 NaviagtionView 之前。

<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。然后,当用户点击抽屉布局项目时,我可以在其中动态加载 fragment ,并且所有这些项目都会有一个漂亮的动画工具栏。到目前为止,在调用的 fragment 中操作时,我无法使工具栏滚动。我想知道这是否可能。

有人做到了吗?非常感谢任何指针。

最佳答案

<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 必须是所有 View 的父级。 DrawerLayout 获取 layout_behavior。由于要与 viewpager 的元素进行交互,因此需要在 DrawerLayout 的顶层 View 中。

关于Android - Coordinator Layout、Drawer Layout 和 Fragments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31918691/

相关文章:

android - Android中设置按钮点击效果

android - ItemTouchHelper 和 SwipeRefreshLayout (RecyclerView)

android - 如何在 Android 设计库中使用 TabLayout 获取当前选中的项目位置

android - 不存在像默认构造一样的创建者): cannot deserialize from Object value (no delegate- or property-based Creator

AndroidPlot - XYplot 未显示在 xml 中的图形布局上

android - 应用程序不显示 firebase 通知(广播 Intent 回调 : result=CANCELLED forIntent)

java - Android 工具栏后退按钮无法识别

android工具栏显示一半内容

android - 无法解析方法 setSupportActionBar(androidx.appcompat.widget.Toolbar)

android - SnackBar 和 fitSystemWindows