0

I have a fragment which has recyclerview inside a Nestedscrollview,also instances of the same fragment is added inside a viewpager which exists in a parent fragment .when an item in the recyclerview is selected parent fragment is replaced with another fragment and on coming back to the parent fragment I want to retain the recycler view scroll position.

The viewpager and recyclerview contents are cached in the viewmodel but the recyclerview scrolls to the top, I tried different ways to save scroll state of recyclerview including saving layoutmanager scroll state in viewmodel but did not work.

Below is the layout file

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/fragmentRootView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary">

            <include
                android:id="@+id/appbar"
                layout="@layout/toolbar_text" />

            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/appbar">

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

                    <Button
                        android:id="@+id/order_notification"
                        style="@style/gilroy_bold"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:layout_marginBottom="2dp"
                        android:background="@color/order_notification_color"
                        android:drawableEnd="@drawable/ic_white_rtl_arrow"
                        android:gravity="center_vertical|start"
                        android:paddingStart="20dp"
                        android:paddingEnd="20dp"
                        android:text="@string/order_notification"
                        android:textAllCaps="false"
                        android:textColor="@color/white"
                        android:textSize="14sp"
                        android:visibility="gone" />

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView"
                        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_below="@id/appbar"
                        android:background="@color/colorPrimary"
                        android:orientation="vertical" />
                </LinearLayout>
            </androidx.core.widget.NestedScrollView>


            <include
                android:id="@+id/loadingIndicator"
                layout="@layout/loading"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/appbar"
                android:visibility="gone" />

            <include
                android:id="@+id/errorViewContainer"
                layout="@layout/fragment_error_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/appbar"
                android:visibility="gone" />
        </RelativeLayout>


    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
halfer
  • 18,701
  • 13
  • 79
  • 158
Raji A C
  • 1,910
  • 4
  • 22
  • 30
  • Hi, maybe you can check [this](https://stackoverflow.com/questions/44407092/saving-scroll-state-of-nestedscrollview) – Aldan Jul 28 '20 at 02:31

0 Answers0