1

I'm trying to implement nested RecyclerView. I did that but parent RecyclerView scroll was not smooth on scroll. I did alot of optimization, but still didn't manage to be successful with scroll till I though of putting parent recyclerview inside NestedScrollView. Scroll is flawless now, but I've a problem.

If I scroll (even tiny bit) my inner RecyclerView (horizontal), I immediately get back to the start [of vertical recyclerview - parent].

This happens one time. Why is that and is it possible to fix it?

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/parent_rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </android.support.v7.widget.RecyclerView>

</android.support.v4.widget.NestedScrollView>
MaaAn13
  • 264
  • 5
  • 24
  • 54

2 Answers2

0

As far as I have understood your problem, you parent RecyclerView gets updated and scrolls to position 0 when the inner horizontal RecyclerView items get updated. You have also stated that the horizontal RecyclerView stays in the same place update the items are updated. In this case, you need to save the state of the parent RecyclerView and then put it back where it was after the items in your horizontal RecyclerView get updated.

// Save state
private Parcelable recyclerViewState;
recyclerViewState = parentRecyclerView.getLayoutManager().onSaveInstanceState();

// Restore state
parentRecyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);

Save the state before you call the API for updating the data in your horizontal RecyclerView and then restore the position of the parent RecyclerView when the update finishes.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Is there an option to do achieve result by xml attributes or in more elegant manner? Also, I'd like to point out, that I don't really have to scroll till last element (when new ones get loaded), I can do tiny scroll and the hop to start will happen. :/ – MaaAn13 Feb 22 '18 at 07:19
  • No, it does not. – MaaAn13 Feb 22 '18 at 07:24
  • Oh, I see. Please edit your question and add the comments in your question, those were asked for clarification so that other people can help as well. – Reaz Murshed Feb 22 '18 at 07:26
0

I got a solution. Basically, what I did was wrap recycler view inside of relative layout and set android:descendantFocusability="blocksDescendants" and now it works good.

<android.support.v4.widget.NestedScrollView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:descendantFocusability="blocksDescendants">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/parent_rv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

        </RelativeLayout>

</android.support.v4.widget.NestedScrollView>
MaaAn13
  • 264
  • 5
  • 24
  • 54