RecyclerView を使用した Fragment で Back した時にスクロール位置を保存する方法
概要
複数の Fragment を replace で遷移します。 遷移元の Fragment (Aと呼ぶ) では RecyclerView を使用しており、スクロールした状態で次の Fragment (Bと呼ぶ) に移動します。 B から Back ボタンで、A に戻った際にスクロール位置を保存する方法です。
確認環境
参考情報
下記の情報が見つかり scrollY を保存する方法を試している途中で、もっと単純な方法が見つかりました。
採用した方法
android:id
属性を設定するとスクロール位置を覚えるようになりました。不思議…。なぜだろう。
<android.support.v7.widget.RecyclerView 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/setting_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:layoutManager="LinearLayoutManager" tools:context=".MainActivity" tools:listitem="@layout/setting_item" />