17
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RecyclerViewでスクロール位置を復元する

Posted at

RecyclerViewでスクロール位置を復元したい場合、だいたい position を保持しておいて、それを使って復元するのが王道な感じですが、別の方法もあるようだったのでメモがてら投稿。

// 保持
val state: Parcelable = recyclerView.layoutManager?.onSaveInstanceState()

// 復元
recyclerView.layoutManager?.onRestoreInstanceState(state)

LayoutManageronSaveInstanceState を呼ぶことで、スクロール位置などの情報を持った Parcelable を取得できるので、それを使う方法です。

onSaveInstanceState の場合、オフセットなども良しなに?計算してくれているようなので、 position を保持する方法よりも、いいかもしれないです。

17
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?