Rezaul Karim
Rezaul Karim

Reputation: 1349

CoordinatorLayout with two RecyclerViews

I am trying to use two RecyclerView in one CoordinatorLayout with appbar layout. But the problem is that second recyclerview is overlaping first recyclerview which should be below the first recyclerview. I am googling for this but has no proper solution. CoordinatorLayout is a super-powered FrameLayout.

My code snippet is given 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>

If recycler view is main child of the CoordinatorLayout then I am getting proper recyclerview behavior. But If use two recyclerview as child of CoordinatorLayout then two recycler view is collides. If I use layout_below at recyclerview2 then data is not showing of recyclerview2 i.e. CoordinatorLayout scrollbar scroll till recyclerview1 data only...

Upvotes: 3

Views: 2300

Answers (3)

natario
natario

Reputation: 25204

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

Why are you using a RelativeLayout here?

You should make clear to yourself which is the scrolling view. Right now, you have:

  • A RelativeLayout : doesn’t scroll.
  • RecyclerView’s with wrap_content height : they don’t scroll either, because their height is that of their content.

So you end up with two big childs (the recyclers) which have to fit into a single, small parent (the relative layout). It makes no sense. Instead, consider using a scrolling parent like 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>

Upvotes: 3

Stas Lelyuk
Stas Lelyuk

Reputation: 548

In second RecyclerView you're using wrong id while aligning:

android:layout_below="@+id/selfieItems"

Change it to:

android:layout_below="@+id/RecyclerView1"

Also, if I understand correctly what you're trying to achieve, you could do it much easier with NestedScrollView which is a part of support library. Just wrap your RelativeLayout into this scrollview.

Upvotes: 0

Humza Malik
Humza Malik

Reputation: 343

Replace layout_below of RecyclerView2 in your code

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

Replace this code with yours and this issue will be resolved

Upvotes: 0

Related Questions