27

My question is just like the question asked here : Scroll behavior in nested RecyclerView with horizontal scroll

Similar to the Google Play store UI, I have a nested RecyclerView(Horizontal) inside a parent RecyclerView(Vertical scroll). The parent RecyclerView is a child of CoordinatorLayout in which the toolbar expands and collapse whenever the parent RecyclerView is being scrolled.

Everything works fine when we touch outside of the child RecyclerView (Check image 1 below) and scroll up then the CollapsingToolbar gets collapsed but when i touch one of the the child RecyclerView and scroll up then the collapsing of CollapsingToolbar doesn't happens.

[The below image shows when we touch between Featured and Trending and scroll-up] enter image description here

[The below image shows when we touch on Featured list and scroll-up] enter image description here

UPDATE :

activity of CoordinatorLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coord_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.ChannelHubOld">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        android:fitsSystemWindows="true">
        <RelativeLayout
            android:background="@color/pkDarkGrey"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="200dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/sidebar_header"/>
        </RelativeLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:theme="@style/ActionBarWidget"/>

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<!-- Parent RecyclerView --->

    <android.support.v7.widget.RecyclerView
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view_pagelayout"
        />

</android.support.design.widget.CoordinatorLayout>
Rafique Mohammed
  • 3,666
  • 2
  • 38
  • 43

4 Answers4

55

Had the same issue. Fixed by setting setNestedScrollingEnabled(false) on the horizontal nested RecyclerViews. It seems like the nested scroll wasn't intercepted properly by the CoordinatorLayout.Behavior when not setting this. Try it out!


NOTE: you also have to add a layout behavior (example: app:layout_behavior="@string/appbar_scrolling_view_behavior" ) to the inner (nested) RecyclerView for this to work

android developer
  • 114,585
  • 152
  • 739
  • 1,270
fast3r
  • 1,298
  • 13
  • 15
  • 2
    yea.. i already solved it! i need to apply layoutBehavior in xml for NestedRecycler and then setNestedScrollingEnabled to false. – Rafique Mohammed Feb 06 '16 at 07:39
  • @fast3r You are my hero. This has to be an accepted answer - works just right. – Konstantin Loginov May 08 '16 at 10:07
  • I have to add that setting it in xml doesn't work for API 19 (and presumably API 17). Set it in code instead for it to really work. – terencey Jan 03 '17 at 11:06
  • @squeeish It's supported in XML only from API 21 and above. – android developer Jan 23 '18 at 12:58
  • 1
    Works for me even without "app:layout_behavior="@string/appbar_scrolling_view_behavior"" on the inner recyclerview. I do have that line in the outer view, though. – Rubberduck Feb 25 '19 at 15:23
  • thank you very much. I was struggling with this for months: https://stackoverflow.com/questions/56509759/bottomsheet-issue-with-nested-scrolling-recyclerview-nestedscrollview. Disabling **nestedscrolling** on all my horizontal recyclerviews worked ! – Mackovich Nov 20 '19 at 13:27
0

Check Creating Collapsing effect. this might help you.

code from link if link gets broken.

<android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"></android.support.v7.widget.Toolbar>

</android.support.design.widget.CollapsingToolbarLayout>
Siddhesh
  • 1,370
  • 11
  • 28
  • add for layout manager like this LinearLayoutManager mLayoutManager = new LinearLayoutManager(this); // use a linear layout manager myUpdatedProductsListRecyclerView.setLayoutManager(mLayoutManager); – Harsha Aug 06 '16 at 06:27
0

If anyone is wondering how to solve the same problem but with inner horizontal ViewPager2, here is the solution:

(viewpager.getChildAt(0) as? RecyclerView)?.isNestedScrollingEnabled = false.

If you're wondering why getChildAt(0), well ViewPager2 is a ViewGroup which contains RecyclerView as its child. So setting isNestedScrollingEnabled = false straight to ViewPager2 - will not work since we need to target RecyclerView.

Jonas
  • 1
  • 1
-5

I have a solution for you and I tried this recently so it should work.

Inside your recyclerview, put in your nestedScrollView instead of the other way around like Scroll behavior in nested RecyclerView with horizontal scroll suggested.

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true"
                >
            </android.support.v4.widget.NestedScrollView>

    </android.support.v7.widget.RecyclerView>
Community
  • 1
  • 1
Simon
  • 19,658
  • 27
  • 149
  • 217
  • hi @Simon Wont the app get crash?and answer in the link u have provided says RecyclerView inside NestedScrollView but ur answer is opposite to it. – Rafique Mohammed Jan 15 '16 at 10:14
  • tried now. App crashed. Throws : Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager – Rafique Mohammed Jan 15 '16 at 11:02
  • add for layout manager like this LinearLayoutManager mLayoutManager = new LinearLayoutManager(this); // use a linear layout manager myUpdatedProductsListRecyclerView.setLayoutManager(mLayoutManager); – Harsha Aug 06 '16 at 06:28