【问题标题】:How to implement Coordinator Layout in a fragment如何在片段中实现协调器布局
【发布时间】:2016-09-04 17:03:57
【问题描述】:

您好,我正在学习使用坐标布局。我想知道如何在片段中实现协调器布局。任何人都可以在这里发布一些教程或示例吗?

这是我的activity_main

    <?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"
    android:fitsSystemWindows="true"
    tools:context="com.example.manish.planetdelhi.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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


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

这是我的片段:

<?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"
        android:fitsSystemWindows="true"
        tools:context="com.example.manish.planetdelhi.MainActivity">

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

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/midu_cover"
                    android:contentDescription="@string/paralax"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />

                <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/ThemeOverlay.AppCompat.Light" />

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

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

            <android.support.v7.widget.RecyclerView
                android:id="@+id/detail_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/fab"
            android:layout_margin="10dp"
            app:layout_anchor="@+id/appbar"
            app:layout_anchorGravity="bottom|right|end"
            android:src="@android:drawable/ic_input_add" />

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

谢谢:)

【问题讨论】:

  • Fragment 可以只是 RecyclerView。我看不到将 CoordinatorLayout 移动到 Fragment 的目的

标签: android android-fragments android-coordinatorlayout


【解决方案1】:

您的实现似乎很好。但是为什么您在活动和片段中使用协调器布局。您应该只将其保存在片段中,只需将您的活动 xml 修改为 -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.manish.planetdelhi.MainActivity">

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

</RelativeLayout>

也是一个很好的教程- http://saulmm.github.io/mastering-coordinator

更新:顺便说一句,只是一个建议-您似乎正在使用可滚动视图(回收器视图)。所以我建议你把recyclerview放在一个fragment中,其余代码放在activity中。

你的活动 xml-

<?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"
        android:fitsSystemWindows="true"
        tools:context="com.example.manish.planetdelhi.MainActivity">

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

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/midu_cover"
                    android:contentDescription="@string/paralax"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />

                <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/ThemeOverlay.AppCompat.Light" />

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

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


        <FrameLayout 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/frame"/>
        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/fab"
            android:layout_margin="10dp"
            app:layout_anchor="@+id/appbar"
            app:layout_anchorGravity="bottom|right|end"
            android:src="@android:drawable/ic_input_add" />

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

还有你的片段 xml-

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_height="match_parent"/>

现在只需在运行时扩展框架布局。

【讨论】:

  • 活动 xml 可能只是一个 fragment 标签 - 不需要 RelativeLayout 或 content_main.xml
  • @cricket_007 你是绝对正确的。 Manish 你也可以按照 cricket_007 的建议进行操作。
  • @VaibhavSharma 谢谢。但是我的工具栏会去哪里呢?
  • @ManishGupta 您已经在片段中指定了工具栏。
  • @VaibhavSharma 如果我有 5 个片段怎么办?所以我必须在每个片段中添加一个单独的工具栏?
猜你喜欢
  • 2015-11-02
  • 2023-03-06
  • 2020-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 2016-03-09
相关资源
最近更新 更多