Two recycleviews in one activity scroll problem - android

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>

Related

Recyclerview with fixed height wont scroll inside nested scroll view

I have a RecyclerView with a fixed height inside a NestedScrollView with a few other layouts inside it. The recycler view won't scroll, but it scrolls fine if I set its height to wrap_content.
I cannot make the RecyclerView use wrap_content because there is an issue with EndlessRecyclerViewScrollListener that it keeps loading data from the server and pushing it into my Adapter even if the user has not scrolled down at all.
Most are suggesting to set nested scrolling to false, but if I disable nested scrolling, the NestedScrollView does not allow me to scroll my RecyclerView. But if I leave nested scrolling enabled, the scroll view does not scroll unless I start touching from outside the RecyclerView.
My layout code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/scoop_background"
tools:context=".module.scoop.timeline.ScoopTimelineFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="92dp">
<ImageView
android:id="#+id/ivTimelineBanner"
android:layout_width="match_parent"
android:layout_height="92dp"
android:layout_margin="0dp"
android:clickable="true"
android:padding="0dp"
android:scaleType="fitXY"
android:src="#drawable/banner_placeholder" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="92dp">
<TextView
android:id="#+id/tvGroupMembership"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="#dimen/text_content" />
<TextView
android:id="#+id/tvGroupName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="#dimen/text_header"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGroupMemberCount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="#dimen/text_content" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#null"
android:paddingStart="#dimen/divider_normal"
android:paddingEnd="#dimen/divider_normal"
android:src="#drawable/ic_chevron_right_white_24dp" />
</LinearLayout>
</FrameLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeRefreshLayoutTimeline"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:overScrollMode="never"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="4dp">
<android.support.v7.widget.CardView
android:id="#+id/cvCreateScoop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/divider_small"
android:layout_marginRight="#dimen/divider_small"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
app:cardCornerRadius="8dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="#dimen/divider_normal">
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:src="#drawable/svg_nav_create_scoop" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingStart="#dimen/divider_normal"
android:text="What's on your mind?"
android:textSize="#dimen/text_content" />
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:padding="#dimen/divider_xsmall"
android:src="#drawable/svg_insert_image" />
</LinearLayout>
</android.support.v7.widget.CardView>
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:overScrollMode="never"
android:nestedScrollingEnabled="false"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="4dp">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
What do I have to modify so that the RecyclerView is scrollable, but the whole NestedScrollView also scrolls along with it, so that if the user is scrolling back up (swiping from top to bottom), the RecyclerView will scroll along with the NestedScrollView, which will bring the layouts above RecyclerView.
To visualize:
The layout I'm trying to achieve is similar to that of facebook's. When you scroll down, the timeline will scroll down, and the search bar with the messenger icon at the top is also scrolled so that it is hidden when scrolling down. When you scroll up, the timeline is being scrolled back up, and showing the search bar again.
I decided to do it differently because I wasn't able to solve it with this method. Instead, I used a CollapsingToolbar and put the other layouts inside it, then removing its background so it does not look like a toolbar, and it seamlessly does what I wanted to, just with a different implementation.

RecyclerView does not show all items inside Fragment

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

Recyclerview within a Scrollview dont coordinate

I have this activity design, which shows a card view inside a scrollView, But under that cardview I need to put a listview or recyclerview. The problem is that I need the scroll of that list to coordinate with the main scroll. Osea that when lower in the content the cardview will disappear and the list of the recyclerview or listview will be crossed.
I need all the content to respond to the main scroll to show up and disappear after titlebar, Which should I change or redesign?
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
app:cardElevation="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/post_coordinator"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/post_bg"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="#drawable/default_profile" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
android:src="#drawable/ic_hot_black"
app:layout_anchorGravity="bottom"
android:layout_marginLeft="16dp"
app:layout_anchor="#id/post_bg"
android:tint="#FFF"
/>
<com.mikhaellopez.circularimageview.CircularImageView
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_anchor="#id/post_bg"
app:layout_anchorGravity="center_horizontal|bottom"
android:src="#drawable/profile"
app:civ_border_color="#FFF"
app:civ_border_width="3dp"
/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
android:src="#drawable/ic_hot_black"
app:layout_anchorGravity="bottom|end"
android:layout_marginRight="16dp"
app:layout_anchor="#id/post_bg"
android:tint="#FFF"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_marginTop="205dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="50dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Prepare your Anus Cersei Lanister, You Gonna Die"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#2d2d2d"
android:textAlignment="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="#string/emilia"
android:textColor="#color/secondary_text"
android:layout_marginTop="10dp"
/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
</android.support.v7.widget.CardView>
<ListView
android:id="#+id/post_comments"
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
</ScrollView>
Add your recyclerview inside NestedScrollview.
Also add recyclerview.setNestedScrollingEnabled(false);
Use android:fillViewport="true" to your ScrollView. ListView that located inside ScrollView works properly.
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
I'd recommend putting the CardView inside the RecyclerView as a header, and then you don't need the top ScrollView, the card and the list items would all be scrollable within the RecyclerView.
There are many ways to add a header to a RecyclerView, see here:
Is there an addHeaderView equivalent for RecyclerView?

Can't scroll RecyclerView

I can't scroll my RecyclerView to the end. It cut off my last element and I completly don't know why. I have another RecyclerView in my project, which also seems to be broken.
Here is my layout code:
<RelativeLayout 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:id="#+id/container"
android:padding="10dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
app:cardCornerRadius="2dp"
app:cardUseCompatPadding="true">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="220dp"
android:orientation="horizontal">
(...)
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="HISTORIA"
android:id="#+id/textView17"
android:layout_below="#+id/cv"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Another broken RecyclerView:
<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"
tools:context="com.praca_inz.Fragments.PetrolFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:scaleType="center"
android:src="#drawable/ic_editor_mode_edit"
android:layout_marginBottom="70dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Any ideas, because I don't have any.
You've got an issue in your first layout. In a RelativeLayout, a match_parent sized element will have exactly the same size as the layout itself. That's the case of your RecyclerView. However, it's not at the top of the layout, as it's below the #+id/textView17 TextView. So you're missing roughly 250~300dp of your RecyclerView (card's + text's height). You should probably use a vertical LinearLayout in this case, that's exactly what they're made for. Just set the RecyclerView's height to 0 and weight to 1.
You should set padding bottom in your RecyclerView. Like this:
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingBottom="20dp" />
I hope help you, and sorry for my bad english.
There was a problem with ViewPager in MainActivity layout. The solution was to set a paddingTop parameter, to 105dp (to cover Toolbar and TabLayout) and delete app:layout_behavior="#string/appbar_scrolling_view_behavior" parameter.
Before:
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
After:
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="105dp"/>
Thank you #oguzhand, for directing me to solution!
Add alignParentBottom="true" to the recyclerView. That should pin it to the bottom edge and the top will be pinned to the textview via the layoutBelow tag you already have in place

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