35

I have to create vertical RecyclerView with nested horizontal RecyclerView in every item. Everything is within CoordinatorLayout. When I scroll by tapping outside nested RecyclerView toolbar hides, but when I scroll parent Recycler by tapping on nested one toolbar stays.

Any help would be appreciated.

Here is my xml layouts:

main_activity.xml:

<android.support.design.widget.CoordinatorLayout 
   ...>

<FrameLayout
    android:id="@+id/fragment_frame"
    ...
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    ...
    android:fitsSystemWindows="true"
    android:id="@+id/appbar_layout">

        <include layout="@layout/toolbar"/>

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

Here is toolbar.xml :

<android.support.v7.widget.Toolbar
    android:id="@+id/main_toolbar"
    ...
    android:fitsSystemWindows="true"
    app:layout_scrollFlags="scroll|enterAlways">

    <TextView .../>

</android.support.v7.widget.Toolbar>

fragment.xml:

<android.support.v7.widget.RecyclerView
    ...
    android:scrollbars="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

And recycler_view_item.xml:

<RelativeLayout ...>

    <TextView .../>

    <!-- fixme(CullyCross) fix bug with hiding toolbar -->
    <android.support.v7.widget.RecyclerView
        ...
        android:scrollbars="horizontal"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

</RelativeLayout>

Thanks,
Anton

Anton Shkurenko
  • 4,301
  • 5
  • 29
  • 64

3 Answers3

64

As requested here is the solution I found good enough so far:

In my case I have a nestedScrollView with 4 RecyclerViews set to scroll horizontally inside. For each of those RecyclerViews I have done this programatically:

restaurantsRecylerView.setHasFixedSize(true); 
restaurantsRecylerView.setNestedScrollingEnabled(false);

You probably don't want the fixedSize, not sure if it will make any difference, my list is always 25 so I can use that for performance. After having done this I can scroll without issues even when I touch on the recyclerViews

Hope it helps

Sam
  • 4,475
  • 2
  • 28
  • 31
Roberto
  • 1,793
  • 1
  • 18
  • 19
  • 3
    setNestedScrollingEnabled helped in my case. Not that you can also set this in xml using android:nestedScrollingEnabled="false" :) – vanomart Dec 23 '16 at 18:30
  • 1
    I disabled the nested scrolling only and it also worked. Thanks. – Prashant Jul 12 '17 at 13:05
  • XML `android:nestedScrollingEnabled="false"` for nested recycler works using Groupie. – MainActivity Nov 20 '19 at 01:28
  • @All I have to create vertical recycler view inside horizontal recyclerview for each item. major part if user scrolling any inner horizontal recycler view I need to scroll all recycler view in each item . I have tried 80 % completed but having problem ................ I need to now is it possible we can stop scrolling in RecyclerView.SCROLL_STATE_SETTLING state – Srinivasan M Jun 06 '20 at 18:37
20

Try with RecyclerView inside android.support.v4.widget.NestedScrollView.

<android.support.v4.widget.NestedScrollView
        android:id="@+id/nScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<!-- Set other views of your Layout -->

</android.support.v4.widget.NestedScrollView>

Also try with different layout_scrollFlags in Toolbar and

RecylerView.setNestedScrollingEnabled(false); // set it true or false as per requirement
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
  • I'm having the exact same issue. I have a list of horizontal recyclerViews all of them inside a NestedScrollView. If you scroll tapping on one of those recyclerViews the coordinator layout doesn't work properly. The toolbar is not hidden or shown, if you tap anywhere else is fine. Also there is a really bad experience when trying to scroll horizontally. – Roberto Nov 16 '15 at 11:57
  • @Lancelot I didn't find the answer that time, if you find it, write an answer here, I'll accept it – Anton Shkurenko Nov 16 '15 at 16:37
  • 3
    Hi @AntonShkurenko this is what I've done and I'm happy with the solution so far. In my case I have a nestedScrollView with 4 RecyclerViews set to scroll horizontally inside. For each of those RecyclerViews I have done this programatically: restaurantsRecylerView.setHasFixedSize(true); restaurantsRecylerView.setNestedScrollingEnabled(false); You probably don't want the fixedSize, not sure if it will make any difference, my list is always 25 so I can use that for performance. After having done this I can scroll without issues even when I touch on the recyclerViews – Roberto Nov 17 '15 at 16:51
  • @Lancelot hm. CoorinatorLayout handles scroll now? Toolbar hides/collapses? etc? – Anton Shkurenko Nov 17 '15 at 21:02
  • @AntonShkurenko Everything works as expected by making that changed. The problem is that those recyclerViews enter in conflict with the coordinator layout and the nestedScrollView. – Roberto Nov 18 '15 at 09:46
  • 1
    @Lancelot I was struggling with this and your solution is what I was looking for. I struggled to find this answer, if you put it as an answer rather than a comment it will probably be easier for other people to find and you will get some rep. At least from me. Thanks. – Daniel Julio Dec 18 '15 at 09:28
  • 2
    Hi @DanielJulio I've just posted as an answer, I'm still trying to make it better as the horizontal scrolling is a bit difficult some times. Thanks for your comment. – Roberto Jan 18 '16 at 16:06
4

We can achieve this in XML

android:nestedScrollingEnabled="false"
pratham kesarkar
  • 3,770
  • 3
  • 19
  • 29