Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 在处理滚动和非滚动片段时,CoordinatorLayout是否应该放在Activity或Fragment中?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
the examples中,对于CoordinatorLayout,它总是放在Activity中,而Fragments都是滚动的.

可以想象一个场景,然加载的某些片段是可滚动的,而某些片段不是,但它们会被加载到活动中的同一个占位符中.在这种情况下,CoordinatorLayout应该放在Activity还是Fragments中?

一方面,因为您将工具栏指定为它的子项,所以它似乎都应该在Activity中.

另一方面,如果你在同一个占位符中加载了滚动和非滚动片段,那么这似乎很难配置(例如你为Fragment占位符指定了什么滚动行为?),所以它似乎应该存在碎片.但是,在这种情况下,每个片段是否必须再次设置工具栏?

解决方法

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

答:您可以在不同情况下的活动或片段中使用COordinatorLayout

问:一方面,所以它似乎都应该在Activity中.

您可以将工具栏添加到片段布局,而不是像下面的活动布局

在这个例子中,我使用NavigationView和viewPager创建活动:

所以为我的活动创建布局(此布局适用于MainActivity):

<android.support.v4.widget.DrawerLayout
xmlns:android="http://scheR_828_11845@as.android.com/apk/res/android"
xmlns:app="http://scheR_828_11845@as.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://scheR_828_11845@as.android.com/apk/res/android"
xmlns:app="http://scheR_828_11845@as.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://scheR_828_11845@as.android.com/apk/res/android"
xmlns:tools="http://scheR_828_11845@as.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_list_content"
xmlns:fab="http://scheR_828_11845@as.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 – 在处理滚动和非滚动片段时,CoordinatorLayout是否应该放在Activity或Fragment中?全部内容,希望文章能够帮你解决android – 在处理滚动和非滚动片段时,CoordinatorLayout是否应该放在Activity或Fragment中?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。