Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two RecyclerViews under each other in one layout

How can I get two RecyclerViews under each other in one layout? I don't want to have a single RecyclerView for all items. My code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@color/main__item_background" android:layout_height="match_parent" android:layout_width="match_parent">  <TextView     android:text="@string/find_friends__already_playing"     android:background="@color/header"     android:gravity="center"     android:layout_width="match_parent"     android:layout_height="@dimen/list_header"     android:visibility="visible"/>  <android.support.v7.widget.RecyclerView     android:id="@+id/in_app_friends"     android:layout_height="wrap_content"     android:layout_width="wrap_content"/>  <TextView     android:text="@string/find_friends__invite_friends"     android:background="@color/find_friends__header"     android:gravity="center"     android:layout_width="match_parent"     android:layout_height="@dimen/list_header" />  <android.support.v7.widget.RecyclerView     android:id="@+id/friends_to_invite"     android:layout_height="wrap_content"     android:layout_width="wrap_content" /> </LinearLayout> 
like image 302
alan_derua Avatar asked May 05 '15 19:05

alan_derua


2 Answers

I've found the answer myself.

You need to put the LinearLayout into a ScrollView and use wrap_content as RecyclerView's layout_height.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillViewport="true">  <LinearLayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="vertical" >      <TextView         android:layout_width="match_parent"         android:layout_height="@dimen/list_header"         android:background="@color/header"         android:gravity="center"         android:text="@string/find_friends__already_playing"         android:visibility="visible" />      <android.support.v7.widget.RecyclerView         android:id="@+id/in_app_friends"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_marginBottom="5dp"         android:background="@color/white"/>      <TextView         android:layout_width="match_parent"         android:layout_height="@dimen/list_header"         android:background="@color/find_friends__header"         android:gravity="center"         android:text="@string/find_friends__invite_friends" />      <android.support.v7.widget.RecyclerView         android:id="@+id/friends_to_invite"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:background="@color/white"/> </LinearLayout> </ScrollView> 

Also there is a bug with with RecyclerView and wrap_content so you have to use a custom layout manager. Check out this post: How do I make WRAP_CONTENT work on a RecyclerView

like image 87
alan_derua Avatar answered Sep 22 '22 23:09

alan_derua


You should create an XML layout file like this

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

And in the code, you should call setNestedScrollingEnabled(false)

RecyclerView ingredientsList = findViewById(R.id.ingredients_list); RecyclerView stepsList = findViewById(R.id.steps_list);  ingredientsList.setNestedScrollingEnabled(false); stepsList.setNestedScrollingEnabled(false); 
like image 22
Ahmet Türk Avatar answered Sep 24 '22 23:09

Ahmet Türk