RecyclerView does not show all items inside Fragment - android

The problem
In my Activity I initialize a Fragment, which contains a RecyclerView. But I've recognized that it is not displaying all items. That means instead of 14 it only shows 11 items. In addition the last visible item (number 12) is cutted off
My Implementation
The Activity contains an empty FrameLayout that acts as the Fragment container:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res./android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e0e0e0">
<LinearLayout
android:id="#+id/layout_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/layout_border_white"
android:paddingBottom="#dimen/footer_padding">
<include layout="#layout/footer"></include>
</LinearLayout>
<FrameLayout
android:id="#+id/fragment_start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/layout_footer"/>
</RelativeLayout>
And here is the RecyclerView in the Fragment:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e0e0e0"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout_header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="#dimen/startpage_padding"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<include layout="#layout/header"></include>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/startpage_margin"
android:scrollbars="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_header"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="#dimen/startpage_progressbar"
android:layout_height="#dimen/startpage_progressbar"
android:visibility="invisible"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="#dimen/startpage_text_padding"
android:text="#string/error_message"
android:textSize="#dimen/startpage_text"
android:visibility="invisible"/>
</LinearLayout>
Solving Approaches
I've already tried it with match_parent in the RecyclerView and also added a paddingBottom to it. With paddingBottom="30dp" one more element is visible but that's not a good solution. Furthermore my Activity is using the AlertDialog-Theme which is setted in the Manifest. Removing it shows the same result.
Any help would be greatly appreciated.

I finally fixed it. The problem was the wrap_content in my RecyclerView inside ConstraintLayout. By using wrap_content for the height of the RecyclerView the parent cuts the bottom of it off. So you have to use 0dp for the height and a constraintBottom.
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="#dimen/startpage_margin"
android:scrollbars="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_header"/>

try to change your xml to following, as constraint layout wraps spaces when used in incorrect way:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#e0e0e0">
<LinearLayout
android:id="#+id/layout_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white">
<include layout="#layout/support_simple_spinner_dropdown_item"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/startpage_margin"
android:scrollbars="vertical"
android:layout_below="+id/layout_header"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_centerInParent="true"
android:visibility="invisible"/>
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Error"
android:textSize="20sp"
android:visibility="invisible"/>
</RelativeLayout>
</LinearLayout>

I used wrap_content for the height but the bottom of the recycleview still scrolled off bottom of the screen so that the last item was not visible. I resorted to using a finagled solution of adding a bottom padding on the recyclerview until the last item was visible

Related

RecyclerView inside of NestedScrollView only showing 1 element at a time

I have a RecyclerView inside of a NestedScrollView, my problem is that the RecyclerView is very short, only large enough to show 1 element at a time. The dataset is small (3-5 elements) so my goal is to show all elements at once.
I've tried disabling nested scrolling, changing the layout_height to wrap_content/match_parent, etc. but to no avail.
Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:paddingBottom="52dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp">
<!-- A bunch of other layouts -->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/documents_rv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:paddingBottom="8dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#id/title"
app:layout_constraintTop_toBottomOf="#id/doc1"
tools:visibility="visible"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Update, item xml (I know it could be replaced with just 1 TextView, but for now it will stay as is):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/doc_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_doc"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/doc_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
android:gravity="center_vertical" />
</LinearLayout>
Not an ideal solution since it uses nested layouts, but I was able to display all items by wrapping my RecyclerView in a RelativeLayout:
<RelativeLayout
android:id="#+id/list_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:paddingBottom="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#id/title"
app:layout_constraintTop_toBottomOf="#id/title">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/documents_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Two recycleviews in one activity scroll problem

I have created 2 recycleviews in one activity. One scrolls horizontally while other scrolls vertically. I can scroll correctly inside each RecyclerView but the page as a whole won't scroll i.e. top RecyclerView stays at the top always and bottom one stays at the bottom like both are fixed in position.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.shakeelnawaz.recipes.AllRecipesFragmet">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="#string/trending_recipes"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontaltrendingrecycleview"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginStart="15dp"
android:orientation="horizontal"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="#string/all_recipes"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>
I read this post "Scolling with multiple RecyclerViews in Layout" and set vertical recycleview's height programmatically. like this
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// calculate height of RecyclerView based on child count
params.height=1150;
// set height of RecyclerView
recyclerView.setLayoutParams(params);
But the problem is that how I can calculate the height of RecyclerView based on child count?
Use NestedScrollView instead of ScrollView
and use this below line in your Fragment in Activity.
ViewCompat.setNestedScrollingEnabled(mYourRecycleView, false);
This will be work on all your Android API level.
Replace ScrollView with NestedScrollView
Then add: horizontaltrendingrecycleview.isNestedScrollingEnabled = false
Your XML should look like below:
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context="com.shakeelnawaz.recipes.AllRecipesFragmet">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="#string/trending_recipes"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontaltrendingrecycleview"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginStart="15dp"
android:orientation="horizontal"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="#string/all_recipes"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

RecyclerView with table like layout and Sticky header -- Originally tried NestedScrollView with no success

I am building a daily schedule like for students. This schedule is in a fragment. I need a sticky header with seven day labels and then the items need to scroll underneath, all at once. In addition I need the entire view to scroll horizontally.
Currently I have a HorizontalScrollView, some nested LinearLayouts, and a NestedScrollView. There are 7 RecyclerViews that are children of the NestedScrollView, one for each day of the week. This is so that I can call setNestedScrollingEnabled(false) on each RecyclerView so they scroll together. Currently the result is that the NestedScrollView clips the RecyclerViews to 1 item and no scrolling is possible. If I remove the NestedScrollView The RecyclerViews all scroll individually which is not what I want.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp" />
<FrameLayout
android:layout_below="#id/toolbar"
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
fragment_schedule.xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ProgressBar
android:id="#+id/loading"
android:layout_width="74dp"
android:layout_height="74dp"
android:visibility="gone"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
...7 ImageViews representing days of week...
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_marginTop="8dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/sunday_list"
android:layout_width="84dp"
android:layout_height="wrap_content"
tools:background="#color/accent_gold"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/monday_list"
android:layout_width="84dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
tools:background="#color/accent_gold"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/tuesday_list"
android:layout_width="84dp"
android:layout_marginLeft="8dp"
android:layout_height="wrap_content"
tools:background="#color/accent_gold"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/wednesday_list"
android:layout_width="84dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
tools:background="#color/accent_gold"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/thursday_list"
android:layout_width="84dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
tools:background="#color/accent_gold"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/friday_list"
android:layout_width="84dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
tools:background="#color/accent_gold"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/saturday_list"
android:layout_width="84dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
tools:background="#color/accent_gold"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</HorizontalScrollView>
The only thing of note in the fragment class is that I call setNestedScrolledEnabled(false) after populating and attaching all of the adapters.
Any insight is greatly appreciated!
The solution in this case is that I was thinking about the requirements too rigidly. I changed my layout to consists of a The header linear layout, a scrollview, and a horizontal recycler view, with each day being an item. In the adapter the item layout is simply a LinearLayout that I programmatically add the class views to(as there is a finite number of classes per day). Scrolling works in both directions. I will retitle the question so hopefully someone can find this working towards similar requirements.
Fragment xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ProgressBar
android:id="#+id/loading"
android:layout_width="74dp"
android:layout_height="74dp"
android:visibility="gone"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
...Image Views Representing Days of Week...
</LinearLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/day_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</ScrollView>
</LinearLayout>
</HorizontalScrollView>
RecyclerViewItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="84dp"
android:layout_height="match_parent"
android:id="#+id/item_schedule_layout"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="0dp">
</LinearLayout>
Then programmatically add views to the LinearLayout. My views for each class were complex so I created a custom view based on the layout I needed for each class and then was able to just define a new instance in a for loop and add it to the LinearLayout.

How to center RecyclerView items horizontally with vertical GridLayoutManager

I have a vertical RecyclerView using a GridLayoutManager. I want each column to be centered, but the columns begin all the way on the left. In the following picture you can see what I'm talking about. I used the ugly color scheme to illustrate the columns and background. The green is the background for each item in the RecyclerView, the red is the background of the RecyclerView itself:
http://imgur.com/a/J3HtF
I'm setting it up with:
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
Here's the column_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="180dp"
android:orientation="vertical"
android:padding="4dp">
<ImageView
android:id="#+id/movie_column_photo"
android:layout_width="80dp"
android:layout_height="120dp"/>
<TextView
android:id="#+id/movie_column_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Here's the recyclerview xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/company_details_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Try letting the column item fill the width of the column while centering everything inside:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:padding="4dp">
<ImageView
android:id="#+id/movie_column_photo"
android:layout_width="80dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/movie_column_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
To restate #kris larson's answer;
If you are using ConstraintLayout, adding android:layout_gravity="center" or android:layout_gravity="center_horizontal" to the parent layout in item's XML would be enough (the first parent in hierarchy).
In your case, the codes would be something similar to this:
<android.support.constraint.ConstraintLayout
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:padding="4dp"
android:layout_gravity="center">
<ImageView
android:id="#+id/movie_column_photo"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="80dp"
android:layout_height="120dp"/>
<TextView
android:id="#+id/movie_column_title"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
Simply set the width of the root layout for your column_item.xml to match_parent will center RecyclerView items horizontally.
Wrap your recyclerview inside the constraintlayout, something like this:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/book_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:spanCount="2" />
Hope it helps!!

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>
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
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);
I also had the same problem and wrote a library which helps to achieve this by joining adapters and layouts.
Gradle dependency to try it (needs jcenter repo):
compile 'su.j2e:rv-joiner:1.0.3'//latest version by now
Thea change xml to use a single RecyclerView which matches parent:
<android.support.v7.widget.RecyclerView
android:id="#+id/joined_friends_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:background="#color/white"/>
Then init RecyclerView in code like this:
//init your RecyclerView as usual
RecyclerView rv = (RecyclerView) findViewById(R.id.joined_friends_rv);
rv.setLayoutManager(new LinearLayoutManager(this));
//construct a joiner
RvJoiner rvJoiner = new RvJoiner();
rvJoiner.add(new JoinableLayout(R.layout.your_title_for_in_app));
rvJoiner.add(new JoinableAdapter(new YourInAppRvAdapter()));
rvJoiner.add(new JoinableLayout(R.layout.your_title_for_invite));
rvJoiner.add(new JoinableAdapter(new YourInviteRvAdapter()));
//set join adapter to your RecyclerView
rv.setAdapter(rvJoiner.getAdapter());
You can check this link for more library details and explanation. Hope it helps.
if you get the bottom recyclerview not scrolling with the main content, change the LinearLayout (see answer from alan_derua) to ConstraintLayout and wrap the two RecyclerViews inside. See code below:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:id="#+id/task_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/first_list_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:gravity="left"
android:paddingTop="0dp"
android:text="#string/my_tasks"
app:layout_constraintBottom_toTopOf="#+id/second_list_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/first_list_view" />
<android.support.v7.widget.RecyclerView
android:id="#+id/second_list_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
This worked for me!
You can give each RecycleView height equal to 0dp and weight equal 1:
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
Use NestedScrollView as parent layout, it should have
android:weightSum="2"
and give
android:layout_weight="1"
to each RecyclerView of yours.It should be scrolled one after each other.
Just use:
<android.support.v7.widget.RecyclerView
android:id="#+id/card_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:scrollbars="vertical"
android:layout_below="#+id/your_first_recycler"/>
last line is for your problem.use it.

Categories

Resources