nested scrollview + recyclerview, strange autoscroll behaviour

Renaud Favier picture Renaud Favier · Jun 22, 2016 · Viewed 14.1k times · Source

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.

Answer

Albert Vila Calvo picture Albert Vila Calvo · Nov 8, 2016

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.