I have a Fragment A (say) which contains a FragmentPagerAdaptor, which further contains Fragments (having list view).

我有一个片段A(比如说),其中包含一个FragmentPagerAdaptor,它还包含片段(具有列表视图)。

When I click on a list item, I replace the complete Fragment A with another fragment (say B), I have also added the transaction for Fragment A in the back stack; now when I press the back button from B, the list in the child Fragment of A gets recreated and scrolled to top.

当我点击列表项时,我用另一个片段(比如B)替换完整的片段A,我还在后栈中添加了片段A的事务;现在,当我按下B中的后退按钮时,子片段A中的列表将被重新创建并滚动到顶部。

I want to retain the state(scroll) of the list, I tried storing the scrollY of the list and setting it again, but it's inaccurate and the list takes some time to initialize, also it takes time to scroll (since the list can have thousands of items).

我想保留列表的状态(滚动),我尝试存储列表的scrollY并再次设置它,但它不准确,列表需要一些时间来初始化,也需要时间滚动(因为列表可以有成千上万的物品)。

I came across this link to resolve the issue:

我遇到了这个链接来解决这个问题:

http://ideaventure.blogspot.in/2014/10/nested-retained-fragment-lost-state.html

but setting the setRetainInstance(true) in child Fragments crashes the app saying:

但在子Fragments中设置setRetainInstance(true)会崩溃应用程序说:

java.lang.IllegalStateException: Can't retain fragements that are nested in other fragments

java.lang.IllegalStateException:无法保留嵌套在其他片段中的fragements

It is also a known bug in google forums:

它也是谷歌论坛中的一个已知错误:

https://code.google.com/p/android/issues/detail?id=74222

Any kind of help will be greatly appreciated!

任何形式的帮助将不胜感激!

1 个解决方案

#1


1

I want to retain the state(scroll) of the list, i have tried storing the scrollY of the list and setting it again, but it's inaccurate and the list takes some time to initialize, also it takes time to scroll(since the list can have thousands of items).

我想保留列表的状态(滚动),我已经尝试存储列表的scrollY并再次设置它,但是它不准确并且列表需要一些时间来初始化,也需要时间来滚动(因为列表可以有成千上万的物品)。

this is the recomended aproach, retained fragments were designed to maintain data not UI elements, also nested fragments have some limitations. There are SO's on how to maintain list scroll position, ie.: Maintain/Save/Restore scroll position when returning to a ListView. So you should be able to find a working solution.

这是推荐的aproach,保留的片段被设计为维护数据而不是UI元素,嵌套片段也有一些局限性。有关如何维护列表滚动位置的信息,即:返回ListView时保持/保存/恢复滚动位置。所以你应该能够找到一个有效的解决方案。

If you have some heavy data structure for your list, you can put it inside non nested retained fragment with no UI. This fragment will not be destroyed during config changes (like screen rotation), but it will still be destroyed when you close your app and Android decides to kill your process. So This may cause crashes if you forgot about this case, you must prepare your app for it.

如果您的列表有一些繁重的数据结构,则可以将其放在没有UI的非嵌套保留片段中。在配置更改期间(例如屏幕旋转),此片段不会被销毁,但是当您关闭应用程序并且Android决定终止您的进程时,它仍会被销毁。因此,如果您忘记了这种情况,可能会导致崩溃,您必须为此准备应用程序。

Other option would be to store you list data in sqlite database, it would be immune to config changes, but probably updates of sqlite might be slow.

其他选项是将列表数据存储在sqlite数据库中,它可以免受配置更改,但可能更新sqlite可能会很慢。

更多相关文章

  1. 根据用户的动态字段对链接列表进行排序
  2. 添加到arraylist中的列表时IndexOutOfBoundsException

随机推荐

  1. 什么?你项目还在用Date表示时间?!
  2. CSS3 全屏大气手风琴图片展示插件
  3. 嗯,用了一下Kotlin编程语言,真香!
  4. 纯CSS3骏马奔腾动画特效
  5. 用 Jest 进行 JavaScript 测试[每日前端
  6. CSS3实现摩天轮动画特效
  7. CSS3和SVG实现的圆环菜单动画
  8. 膜拜!感谢大佬们赏饭吃,向你们致敬!
  9. 使用 React 要懂的 JavaScript 特性[每日
  10. Android使用Canvas绘图