【问题标题】:CoordinatorLayout with two RecyclerViews带有两个 RecyclerViews 的 CoordinatorLayout
【发布时间】:2016-05-25 12:06:38
【问题描述】:

我正在尝试在一个带有 appbar 布局的 CoordinatorLayout 中使用两个 RecyclerView。 但问题是 第二个 recyclerview 与第一个 recyclerview 重叠,第一个 recyclerview 应该低于第一个 recyclerview。我正在为此搜索,但没有适当的解决方案。 CoordinatorLayout 是一个超级强大的 FrameLayout。

我的代码 sn-p 如下:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="366dp"
        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:background="@drawable/selfie_collasping_toolbar_background"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/colorPrimary_selfie"
            app:expandedTitleMarginBottom="255dp"
            app:expandedTitleMarginStart="70dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="Check ins">

            <RelativeLayout
                android:id="@+id/collapsing_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="70dp"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">

                <ImageView
                    android:id="@+id/offer_gift"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="15dp"
                    android:layout_marginTop="5dp"
                    android:src="@mipmap/image" />             

            </RelativeLayout>

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/RecyclerView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:layout_marginTop="15dp" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/RecyclerView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/RecyclerView1"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:layout_marginTop="15dp" />
    </RelativeLayout>

    <com.getbase.floatingactionbutton.FloatingActionsMenu
        android:id="@+id/fab"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom|right">

     ---

    </com.getbase.floatingactionbutton.FloatingActionsMenu>
</android.support.design.widget.CoordinatorLayout>

如果回收器视图是 CoordinatorLayout 的主要子视图,那么我将获得正确的回收器视图行为。但是如果使用两个回收器视图作为 CoordinatorLayout 的子级,那么两个回收器视图会发生碰撞。如果我在 recyclerview2 使用 layout_below,则数据不会显示 recyclerview2,即 CoordinatorLayout 滚动条滚动到仅 recyclerview1 数据...

【问题讨论】:

  • 你能详细说明你想要实现什么吗?

标签: android android-recyclerview android-coordinatorlayout


【解决方案1】:
<RelativeLayout
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView1"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView2"
        android:layout_height="wrap_content"
        android:layout_below="@+id/RecyclerView1" />
</RelativeLayout>

你为什么在这里使用RelativeLayout?

您应该清楚自己是滚动视图。现在,你有:

  • RelativeLayout:不滚动。
  • RecyclerViewwrap_content 高度:它们也不滚动,因为它们的高度是它们的内容的高度。

所以你最终得到了两个大孩子(回收者),它们必须适合一个单一的小父母(相对布局)。这没有道理。相反,请考虑使用像 NestedScrollView 这样的滚动父项:

<android.support.v4.widget.NestedScrollView
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior”>
    <LinearLayout
        android:layout_height=“wrap_content”
        android:orientation=“vertical”>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/RecyclerView1"
            android:layout_height="wrap_content" />
        <android.support.v7.widget.RecyclerView
            android:id="@+id/RecyclerView2"
            android:layout_height="wrap_content" />

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

【讨论】:

    【解决方案2】:

    在你的代码中替换 RecyclerView2 的 layout_below

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:fab="http://schemas.android.com/apk/res-auto"
        android:id="@+id/layout_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="366dp"
            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:background="@drawable/selfie_collasping_toolbar_background"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/colorPrimary_selfie"
                app:expandedTitleMarginBottom="255dp"
                app:expandedTitleMarginStart="70dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:title="Check ins">
    
                <RelativeLayout
                    android:id="@+id/collapsing_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="70dp"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="parallax"
                    app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
    
                    <ImageView
                        android:id="@+id/offer_gift"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_marginLeft="15dp"
                        android:layout_marginTop="5dp"
                        android:src="@mipmap/image" />             
    
                </RelativeLayout>
    
                <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>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/RecyclerView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="3dp"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:layout_marginTop="15dp" />
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/RecyclerView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/RecyclerView1"
                android:layout_marginBottom="3dp"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:layout_marginTop="15dp" />
        </RelativeLayout>
    
        <com.getbase.floatingactionbutton.FloatingActionsMenu
            android:id="@+id/fab"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom|right">
    
         ---
    
        </com.getbase.floatingactionbutton.FloatingActionsMenu>
    </android.support.design.widget.CoordinatorLayout>
    

    将此代码替换为您的代码,此问题将得到解决

    【讨论】:

    • RecyclerView2 的替换 layout_below 不起作用... RecyclerView2 数据隐藏,设备屏幕仅显示数据 RecyclerView1。我已经尝试过使用这个......一些坐标布局滚动在 RecyclerView2 数据之前不会滚动
    【解决方案3】:

    在第二个 RecyclerView 中,您在对齐时使用了错误的 id:

    android:layout_below="@+id/selfieItems"
    

    改成:

    android:layout_below="@+id/RecyclerView1"
    

    另外,如果我正确理解您想要实现的目标,您可以使用支持库的一部分 NestedScrollView 更轻松地完成它。只需将您的 RelativeLayout 包装到此滚动视图中即可。

    【讨论】:

    • 我重新更正了这个问题..我也尝试使用 NestedScrollView 但不起作用...
    • @Rezaul 在 NestedScrollView 中尝试使用 LinearLayout 而不是 RelativeLayout,让我知道你有什么。不要忘记将方向设置为垂直,将高度设置为 wrap_content。
    • 我已按照您的建议更改为 NestedScrollView 内的相对布局,但两个 recyclerview 数据都隐藏了...
    猜你喜欢
    • 2016-08-11
    • 1970-01-01
    • 2021-10-29
    • 2015-11-08
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    相关资源
    最近更新 更多