Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Recyclerview scrolls by itself

I have a parent recyclerview that has 3 child view in it. The last two of the child are recyclerview.

Parent recyclerview
 - child view 1
 - child view 2 (horizontal rv)
 - child view 3 (horizontal rv)

The issue is every time this fragment is visible, it scrolls itself to align with child view 2's bottom.

I have set the parent rv to listen for scroll. This is what I end up with:

dy: 108
dy: 72
dy: 75
dy: 62
dy: 48
dy: 42
dy: 34
dy: 27
dy: 22
dy: 16
dy: 12
dy: 10
dy: 7
dy: 5
dy: 3
dy: 3
dy: 1
dy: 1
dy: 1

It seems like the starting dy of parent recyclerview is set to 0 to the child view 2 rv. Everything above it is in -ve value. However, I'm not sure if this was the case as I'm still finding out what causes it.

Any fix?

like image 297
emen Avatar asked Aug 15 '16 03:08

emen


People also ask

What is nested scrolling in RecyclerView?

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child. In your case you have to define your own scrolling behaviour.

Is nestedScrollingEnabled false?

Bug: android:nestedScrollingEnabled="false" causes crash on RecyclerView, yet setNestedScrollingEnabled(false) doesn't.

How can show all items in RecyclerView without scrolling?

It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.

How do I stop refreshing RecyclerView data scroll to top position android?

Make a setDataList method in your adapter class. And set your updated list to adapter list. And then every time of calling API set that list to setDataList and call adapter. notifyDataSetChanged() method of your adapter class.


4 Answers

We have a similar problem. We have a vertical RecyclerView. Each item of this vertical RecyclerView contains an horizontal RecyclerView, like in the Android TV app.

When we upgraded the support libs from 23.4.0 to 24.0.0 the automatic scroll suddenly appeared. In particular, when we open an Activity and we then go back, the vertical RecyclerView scrolls up so that the current horizontal RecyclerView row does not get cut and the row is displayed completely.

There is an easy fix. Add this to your outer/parent RecyclerView:

android:descendantFocusability="blocksDescendants"

I've found the solution in this questions:

  • nested scrollview + recyclerview, strange autoscroll behaviour
  • Android prevent nested recyclerview from automatically repositioning

Additionally, I've found another solution, which also works. In our case the vertical RecyclerView is contained inside a FrameLayout. If I add android:focusableInTouchMode="true" to this FrameLayout, the problem goes away.

By the way, there is also an open issue on the AOSP.

like image 106
Albert Vila Calvo Avatar answered Oct 16 '22 22:10

Albert Vila Calvo


Ah, I've been struggling for a fix. The solution is very simple actually. As a reference for me (and anyone else facing the same issue in the future), I just have to setFocusable() in the child view's rv to false, and it doesn't focus to that view anymore when the fragment is visible.

In my case, I have to set it programmatically after data has been loaded from an API.

like image 32
emen Avatar answered Oct 17 '22 00:10

emen


Try this android:descendantFocusability="blocksDescendants". It solved the problem for me.

like image 3
Lehi Lima Avatar answered Oct 16 '22 22:10

Lehi Lima


After a long search i got my eyes on parameter reverseLayout that was set to true. I replaced

horizontalRV.setLayoutManager(
    new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true));

with

horizontalRV.setLayoutManager(
    new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
like image 2
Prakash Kumar Yadav Avatar answered Oct 16 '22 22:10

Prakash Kumar Yadav