Search code examples
androidandroid-recyclerviewandroid-coordinatorlayout

Coordinator Layout with 2 Recycler Views


I want to build an android fragment with this layout:

  1. Top half of screen is a RecyclerView that scrolls HORIZONTALLY.
  2. Bottom half of screen is a RecyclerView that scrolls VERTICALLY.

When the bottom RecyclerView is scrolled toward its bottom, I want the top one to collapse and hide (and open when the bottom view is scrolled to the top).

A coordinator layout would seem to be the answer, but every example I come across uses an AppBarLayout for the top section. The activity containing the fragment already has an app bar displayed; I don't want to modify it.

How would I implement this two RecyclerView setup in a CoordinatorLayout without addressing the app bar?


Solution

  • You can use a NestedScrollView, a LinearLayout with vertical orientation as child of of NestedScrollView and add both RecyclerViews as children of LinearLayout

    sample

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>