34

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?

emen
  • 5,201
  • 10
  • 52
  • 88

7 Answers7

170

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:

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.

Community
  • 1
  • 1
Albert Vila Calvo
  • 12,993
  • 4
  • 53
  • 63
  • 2
    To be clear, you should add this to the parent of the `RecyclerView` which scrolls automatically. – Pei Nov 30 '17 at 17:39
  • 1
    curiously enough it happened for me only in release versions, debug version was fine – Kibotu Apr 03 '18 at 08:23
  • 1
    For me adding ```android:focusableInTouchMode="true"``` to the ```NestedScrollView``` that is the parent of the ```RecyclerView``` fixed the problem. Are there any side effects after adding this code? – marios-codes Feb 16 '19 at 17:30
  • it is not working if you have EditText in your recyclerView. it will make your EditText just blinking when do onClick – AndriiSG Aug 23 '20 at 05:42
3

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.

emen
  • 5,201
  • 10
  • 52
  • 88
  • This has solved it. Thanks. You just need to make sure that if the root recyclerview instances are the ones that should be flagged with focusable as false. – Pier Betos Oct 05 '16 at 15:13
2

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

Vladimir Jovanović
  • 2,123
  • 2
  • 20
  • 39
Lehi Lima
  • 21
  • 3
2

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));
clemens
  • 14,173
  • 11
  • 38
  • 52
0

Short hint to @albertvilacalvo answer: If you use RecyclerView programmatically, you may handle setFocusableInTouchMode that way (include in your RecyclerView class)

@Override
protected void onAttachedToWindow() {
   ((View) getParent()).setFocusableInTouchMode(true);
   super.onAttachedToWindow();
}

The descendantFocusability solution fails if you need EditText in your child views, they would not get cursor anymore. I'm using ListView, custom (multi)touch and EditText in my RecyclerView childs. No sideeffects using setFocusableInTouchMode so far.

allofmex
  • 349
  • 3
  • 11
0

Initialize layout manager in oncreate only or only 1 time dont re initialize it recyclerview.setLayoutManager(new LinearLayoutManager(context));

Sadique Khan
  • 69
  • 1
  • 6
0

Try to get rid of ConstraintLayout if you have nested RecyclerViews. I've just replaced ConstraintLayout with good old LinearLayout and unwanted scroll disappeared!

Hope this might help someone :)

Oleksandr Nos
  • 244
  • 3
  • 14