简体   繁体   English

从另一个活动返回活动时,RecyclerView 的滚动位置丢失

[英]Scroll position of RecyclerView lost when returning back to activity from another activity

I m using Firebase RecyclerView.Apdapter with Firebase UI in MainActivity.我在 MainActivity 中将 Firebase RecyclerView.Apdapter与 Firebase UI 一起使用。 When I'm opening another activity and returning back to main activity, scroll position moves to top.当我打开另一个活动并返回主活动时,滚动位置移动到顶部。 How to have same scroll position after closing of other activity using Firebase RecyclerView?使用 Firebase RecyclerView 关闭其他活动后如何具有相同的滚动位置?

To solve this, you need to know that the component responsible one for holding the layout state (and restoring it) is the LayoutManager used in your RecyclerView .为了解决这个问题,您需要知道负责保持layout state (并恢复它)的组件是RecyclerView使用的LayoutManager To achieve this, you can override the onSaveInstanceState() method and store the position directly in the Bundle object.为此,您可以覆盖onSaveInstanceState()方法并将位置直接存储在Bundle对象中。

To get it back, just override the onRestoreInstanceState() method and get the position back fron the Bundle object.要取回它,只需覆盖onRestoreInstanceState()方法并从Bundle对象取回位置。

In the end just simply use the following line of code in your onStart() method:最后只需在onStart()方法中使用以下代码行:

layoutManager.onRestoreInstanceState(yourListState);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM