Android,DrawerLayout + Fragments + CollapsingToolbarLayout [英] Android, DrawerLayout + Fragments + CollapsingToolbarLayout

本文介绍了Android,DrawerLayout + Fragments + CollapsingToolbarLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DrawerLayout的主容器中显示的片段中是否可以有一个CoordinatorLayout/CollapsingToolbarLayout?

Is it possible to have a CoordinatorLayout / CollapsingToolbarLayout in the fragments shown in the main container of a DrawerLayout?

对另一个问题的答案表明,每个片段可以有自己的工具栏.但这与

An answer to another question suggests that each fragment could have its own Toolbar. But this doesn't work well with the ActionBarDrawerToggle as it requires a Toolbar to link to the open/close drawer behaviour.

有人实现了这一目标吗?或者您对此有何指点?谢谢.

Has anybody achieved this, or do you have pointers about this? Thanks.

编辑:我一直在集中精力将单个工具栏放置在DrawerLayout中,该工具栏始终保持在该位置,但无法使其滚动(在Nexus5 API 22).在此问题中,提到CoordinatorLayout需要成为主视图.因此,也许将其插入DrawerLayout(如下所示)中是行不通的.

EDIT: I've been focusing some efforts in putting a single Toolbar in the DrawerLayout, meant to stay there all the time, but was not able to get it to scroll (on a Nexus5 API 22). In this question it is mentioned that the CoordinatorLayout needs to be the main view. So maybe inserting it in a DrawerLayout (as below) is not going to work.

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

    <!-- main content -->
    <android.support.design.widget.CoordinatorLayout ...>

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

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

                <ImageView .../>

                <android.support.v7.widget.Toolbar .../>

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

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

        <android.support.v7.widget.RecyclerView .../>

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

    <!-- navigation drawer -->
    <android.support.design.widget.NavigationView ...>

        <!-- drawer content -->
        <fragment .../>

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

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

推荐答案

事实上,可以.我正在寻找相同的东西,并且您问题的链接将我引至 Google Blogpost

As a matter of fact, yes you can. I was looking for the same thing and the link from your question lead me to this from Google Blogpost

无论如何,以下是我的布局文件,至于Java代码,我什么都没有更改,并且Fragment调用保持不变.

Anyhow, below are my layout files, as for java code I did not change anything and Fragment calling remains the same.

activity_main.xml(主文件)

activity_main.xml (Main file)

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

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

app_bar_main.xml(NavigationBar布局)

app_bar_main.xml (NavigationBar layout)

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="192dp"
        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"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <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_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

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

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

content_main.xml

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/content_main_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>

现在您有了" DrawerLayout + Fragments + CollapsingToolbarLayout "

这篇关于Android,DrawerLayout + Fragments + CollapsingToolbarLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆