Scroll behavior in nested RecyclerView with horizontal scroll
Asked Answered
P

3

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

Peripteral answered 11/9, 2015 at 14:0 Comment(4)
Any luck? i am facing the same issue!!Typify
@RafiqueMohammed nope, sorryPeripteral
@AntonShkurenko did you get the solution.. I'm face the same issue..Rubberneck
@Rubberneck nope, I didn't, I left that job :) Try the solution below, it seems to be true (I didn't check), if it's true just upvote itPeripteral
C
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

Colunga answered 18/1, 2016 at 16:5 Comment(4)
setNestedScrollingEnabled helped in my case. Not that you can also set this in xml using android:nestedScrollingEnabled="false" :)Campagna
I disabled the nested scrolling only and it also worked. Thanks.Chinkiang
XML android:nestedScrollingEnabled="false" for nested recycler works using Groupie.Latish
@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 stateArchi
F
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
Farah answered 19/9, 2015 at 5:30 Comment(7)
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.Colunga
@Lancelot I didn't find the answer that time, if you find it, write an answer here, I'll accept itPeripteral
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 recyclerViewsColunga
@Lancelot hm. CoorinatorLayout handles scroll now? Toolbar hides/collapses? etc?Peripteral
@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.Colunga
@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.Viburnum
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.Colunga
J
4

We can achieve this in XML

android:nestedScrollingEnabled="false"
Jehiah answered 7/3, 2018 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.