34

In a view pager I have several fragments, one of them uses a nested scrollview with a header and a recyclerview :

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.m360.android.fragment.Members.MemberDetailsFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="20dp">

        <header/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingTop="0dp" />

    </LinearLayout>

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

The tag "header" represents a complex layout that I didn't want to post here as it stretches out the code a lot.

when I switch between the tabs, it scrolls strait to the recycler view. The header is hidden, I have to scroll up to see it.

Any ideas on what causes that ? I don't wanna use a type in my adapter if I can avoid it.

Renaud Favier
  • 1,575
  • 1
  • 14
  • 32

2 Answers2

30

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.

Adding android:descendantFocusability="blocksDescendants" fixes the issue.

However, 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.

There's even a third solution mentioned here, which basically consists on calling setFocusable(false) on the child/inner RecyclerViews. I haven't tryied this.

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

Community
  • 1
  • 1
Albert Vila Calvo
  • 12,993
  • 4
  • 53
  • 63
  • Did you opt for the second solution ? why do you think it's better ? thanks – Renaud Favier Nov 08 '16 at 17:41
  • @RenaudFavier I opded for the `android:descendantFocusability="blocksDescendants"` option. I think that the 3 solutions presented do something similar (basically prevent the child/inner `RecyclerView` to gain focus, which prevents the autoscroll). However, [take a look at this](https://code.google.com/p/android/issues/detail?id=81854#c11) to better understand the issue. – Albert Vila Calvo Nov 08 '16 at 18:00
  • @RenaudFavier I can use the `android:focusableInTouchMode="true"` trick to steal focus because I have my outer `RecyclerView` wrapped inside a `FrameLayout`. I'm not sure that you can do this if the outer `RecyclerView` is the root of your layout. – Albert Vila Calvo Nov 08 '16 at 18:08
  • Ok I see. Thank you for sharing ! – Renaud Favier Nov 08 '16 at 18:45
  • Ok, thats resolved my problem. But WHY this did the trick its the part the I don't get it. If someone could explain it. Thanks! – Felipe Castilhos Feb 19 '18 at 20:17
9

setandroid:focusableInTouchMode="true" for child Layout( may be LinearLayout ) of NestedScrollView

Community
  • 1
  • 1
Balu Sangem
  • 682
  • 4
  • 14