【问题标题】:Scroll position of RecyclerView lost when returning back to activity from another activity从另一个活动返回活动时,RecyclerView 的滚动位置丢失
【发布时间】:2018-03-01 18:08:42
【问题描述】:

我在 MainActivity 中使用 Firebase RecyclerView.Apdapter 和 Firebase UI。 当我打开另一个活动并返回主要活动时,滚动位置移动到顶部。使用 Firebase RecyclerView 关闭其他活动后如何获得相同的滚动位置?

【问题讨论】:

    标签: java android firebase android-recyclerview


    【解决方案1】:

    要解决这个问题,您需要知道负责保存layout state(并恢复它)的组件是您的RecyclerView 中使用的LayoutManager。为此,您可以重写onSaveInstanceState() 方法并将位置直接存储在Bundle 对象中。

    要取回它,只需重写 onRestoreInstanceState() 方法并从 Bundle 对象取回位置。

    最后只需在 onStart() 方法中使用以下代码行:

    layoutManager.onRestoreInstanceState(yourListState);
    

    【讨论】:

      猜你喜欢
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      相关资源
      最近更新 更多