【问题标题】:CoordinatorLayout + FrameLayout + ViewPager + RecyclerViewCoordinatorLayout + FrameLayout + ViewPager + RecyclerView
【发布时间】:2018-09-13 08:48:17
【问题描述】:

我的应用:

我的根布局有底部导航栏,NavigationBar 的每个项目都对应一个FrameLayout,它加载带有ViewPagers 的片段,还有几个带有RecyclerViews 的片段。

我想在我的根布局中实现CollapsingToolbarLayout,当RecyclerView 在任何ViewPagerFragments 中滚动时,我想折叠折叠工具栏。

我的问题是,当ViewPagerCoordinatorLayout 的子级时,我可以实现此行为,但当FrameLayoutCoordinatorLayout 的子级时则不行。我需要实现FrameLayout,否则我必须在每个BottomBar 的项目片段布局中编写工具栏代码。

这是我的根布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#4DBC76"
            app:elevation="0dp">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways">

                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="60dp">

                </android.support.constraint.ConstraintLayout>

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


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

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="125dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:id="@+id/testRootFragment"
            >
        </FrameLayout>


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

    <bd.com.xbit.eat.bottom_navigation
        android:id="@+id/bottomNavigationViewTest"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        app:elevation="25dp"
        app:itemTextColor="@drawable/bottom_selector"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation" />

</android.support.constraint.ConstraintLayout>

这是我的 viewpager 布局代码,它驻留在框架布局中调用的片段中:

<android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/talesTabLayout"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/talesTabLayout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

这是我在Fragment 布局中的RecyclerView

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

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

我在FramelayoutViewPagerRecyclerView 中使用了“appbar_scrolling_view_behavior”属性,但没有成功。我用谷歌搜索了很多,但找不到任何关于这个特定问题的参考。任何形式的帮助将不胜感激!

【问题讨论】:

    标签: android android-recyclerview android-viewpager android-coordinatorlayout android-framelayout


    【解决方案1】:

    首先将app:layout_scrollFlags="scroll|exitUntilCollapsed" 用于CollapsingToolbarLayout 以实现正确的行为。

    我想在我的根布局中实现CollapsingToolbarLayoutRecyclerView 在任何ViewPagerFragments 内滚动时, 我想折叠Toolbar

    我没有在您的布局中看到任何 Toolbars,但是,我假设您在 AppBarLayout 中使用了它,这将有助于实现您想要的:

    app:layout_scrollFlags="scroll|enterAlways"
    
    • 在您的根布局中添加Toolbar

    我的问题是,当ViewPager 是孩子时,我可以实现此行为 CoordinatorLayout 但不是当 FrameLayoutCoordinatorLayout。我需要实现FrameLayout 否则我必须 在每个BottomBar 的项目Fragment 的布局中编写Toolbar 代码。

    在这种情况下使用FrameLayoutCoordinatorLayout)似乎是最好的选择。


    但是,我会做其他事情。我不会在Fragment 或其他内容中添加Fragments,而是在Toolbar 下方插入一个TabLayout,其中包含多个Fragments,就像我的这个例子一样:https://github.com/LinX64/AndroidSupportDesign-TabLayout

    这就是它应该被设计的方式。彼此之间不需要视图,这可能会导致问题。

    顺便说一句,设计不错。

    【讨论】:

    • 非常感谢您的建议。我的问题是我的根布局有由底栏初始化的片段,这些片段有 viewpagers,也有带有 recyclerview 的片段。因此,我试图在我的根目录中使用带有可滚动行为的框架布局。否则我一定会效仿你的。
    猜你喜欢
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    相关资源
    最近更新 更多