android - 在处理滚动和非滚动 fragment 时,CoordinatorLayout 应该放在 Activity 还是 Fragment 中?

标签 android android-coordinatorlayout

the examples对于 CoordinatorLayout,它一直放在 Activity 中,Fragments 都是滚动的。

可以合理地想象这样一种情况,虽然正在加载的一些 fragment 是可滚动的,而另一些 fragment 不是,但它们被加载到 Activity 中的同一个占位符中。这种场景下,CoordinatorLayout应该放在Activity中还是放在Fragments中?

一方面,因为你把Toolbar指定为它的child,看起来它应该都在Activity中。

另一方面,如果您在同一个占位符中同时加载了滚动和非滚动 fragment ,那么这似乎很难配置(例如,您为 fragment 占位符指定了什么滚动行为?),所以它看起来像应该住在 fragment 中。但是,在那种情况下,是否每个 fragment 都必须再次设置工具栏?

最佳答案

问:CoordinatorLayout应该放在Activity中还是放在Fragment中?

A:您可以在不同情况下在activity或fragment中使用CoordinatorLayout

Q:一方面,因为你把Toolbar指定为它的child,所以看起来它应该都在Activity中。

您可以将工具栏添加到 fragment 布局而不是像下面这样的 Activity 布局

在此示例中,我使用 NavigationView 和 viewPager 创建 Activity :

因此为我的 Activity 创建布局(此布局用于 MainActivity):

<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">

<FrameLayout

    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</FrameLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_height="match_parent"
    android:layout_width="240dp"
    android:layout_gravity="right"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_view"
    app:itemIconTint="@color/tint_item_color"
    app:itemTextColor="@color/tint_item_color"
    />

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

为了使用 tablayout 创建 viewPager,我创建了这个布局(这个布局用于 fragmentViewPager):

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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">

    <include layout="@layout/toolbar_main"
        android:id="@+id/toolbar"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/tab_color"
        app:tabSelectedTextColor="@color/tab_color"

        />

</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.CoordinatorLayout>

然后使用我在 viewPager 中使用的 listFragment 的另一个布局:

<android.support.design.widget.CoordinatorLayout
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:id="@+id/main_list_content"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@color/background_window">



<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="0dp"
    android:id="@+id/exersice_listviwe"/>




<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="64dp"
    android:clickable="true"
    android:src="@drawable/ic_done"
    fab:layout_anchor="@+id/main_list_content"
    fab:layout_anchorGravity="bottom|right"
    />


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

关于android - 在处理滚动和非滚动 fragment 时,CoordinatorLayout 应该放在 Activity 还是 Fragment 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31181624/

相关文章:

android - 无法将协调器布局与约束布局一起使用

android - 如果 Activity 已经在前面,则通知中的 PendingIntent 不会触发

android - (0,0) 位于屏幕中间的 View 的坐标

android - 协调器布局自定义布局行为永远不会被调用

Androidx ViewPager 不适用于 ScrollView 行为

android - 使用 tablayout 折叠工具栏

android - 单击/触摸 EditText 时禁用默认键盘

java - setimagebitmap 后 imageview 的高度和 View 搞砸了

java.sql.SQLException : Network error IOException: failed to connect ETIMEDOUT 异常

android - 通过代码使 ListView 项发光