Ronak Makwana
Ronak Makwana

Reputation: 198

Custom GridLayoutManager providing a RecyclerView with WRAP_CONTENT

I have a Fragment that contains two RecyclerViews (one in grid of two columns and another on for my actual feed).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:scrollbars="none"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/second_recycler_view"
    android:scrollbars="none"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/recycler_view"
    />

How do I implement two RecyclerViews in one layout?

Upvotes: 0

Views: 860

Answers (1)

rqiu
rqiu

Reputation: 46

Try to use a single RecyclerView with a GridLayoutManager with custom SpanSizeLookup:

    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
    GridLayoutManager.SpanSizeLookup columnSpanner = new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return adapter.getItemViewType(position) == R.layout.your_two_column_row_layout ? 1 : 2;
        }
    };
    gridLayoutManager.setSpanSizeLookup(columnSpanner);
    recyclerView.setLayoutManager(gridLayoutManager);

and use different holder for different type of row in your adapter.

Upvotes: 1

Related Questions