安卓:Viewpager和FragmentStatePageAdapter [英] Android: Viewpager and FragmentStatePageAdapter

查看:141
本文介绍了安卓:Viewpager和FragmentStatePageAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计一个应用程序,允许用户在ViewPager多个页面间切换。我一直在努力试图弄清楚它是如何可能从网页删除片段实例时,它在屏幕上不再可见,它缓存(,比如说,一个HashMap),然后将其还原,这样,当用户翻转回到那个页面,意见和一切的将是在相同状态删除前。例如,我的第一个页面是一个登录界面,使特定页面上的某些布局元素显示/隐藏在成功登录。当我不停地翻转着足够的页,然后翻转回第一页,布局复位。这将成为更大的问题的另外一个我的网页,其中包含了巨大的,水平/垂直滚动数据网格,我使用一个线程在后台初始化时绘制。我用一个进度对话框通知加载进度的用户,并且成为真烦人,每次我都加载它。

I'm designing an app that allows users to flip between multiple pages in a ViewPager. I've been struggling trying to figure out how it is possible to remove a Fragment instance from a page when it is no longer visible on screen, cache it (to, say, a HashMap), and then restore it so that when the user flips back to that page, the views and everything else in it will be in the same state it was before removal. For example, my first page is a login screen that makes certain layout elements on that particular page visible/invisible on a successful login. When I flip forward enough pages then flip back to the first page, the layout is reset. This becomes more of a problem for another one of my pages which contains a huge, horizontal/vertical scrolling grid of data that I use a thread in the background to draw when it initializes. I use a progress dialog to notify the user of loading progress and that becomes really annoying everytime I have to load it.

所以,我做了一些研究...

So I did some research...

我通过源$ C ​​$下FragmentStatePageAdapter和destroyItem()回调浏览,片段实例被删除的状态保存到一个ArrayList。当该片段的新实例被在instantiateItem()回调创建的,如果不存在的项目的实例(它们跟踪这通过使用一个ArrayList),一个新的片段实例被创建和其保存的状态与相应Fragment.SavedState数据初始化。不幸的是,这个数据不包括国家的意见均不过我注意到,与一个GridView / ListView控件的页面时,浏览的状态正在慢慢地恢复了(如果我滚动到一些随机的位置,翻转了几页,回来,它不会被重置)。

I browsed through the source code for FragmentStatePageAdapter and in the destroyItem() callback, the state of the Fragment instance being removed is saved to an ArrayList. When a new instance of the Fragment is being created in the instantiateItem() callback, if an instance of an item doesn't already exist (they keep track of this by using an ArrayList), a new Fragment instance is created and its saved state is initialized with the corresponding Fragment.SavedState data. Unfortunately, this data does not include the state that the Views were in although I noticed that for pages with a GridView/ListView, the state of the Views were somehow restored (if I scrolled to some random position, flipped a few pages and came back, it would not be reset).

根据API:

保存的状态不能包含其他片段的依赖关系 -   这是它不能使用putFragment(捆绑,串,片段)来存储   片段引用,因为该引用可能是无效的,当这   保存的状态以后使用。同样,片段的目标和结果   code的不包括在该状态

The saved state can not contain dependencies on other fragments -- that is it can't use putFragment(Bundle, String, Fragment) to store a fragment reference because that reference may not be valid when this saved state is later used. Likewise the Fragment's target and result code are not included in this state.

作为一个菜鸟到Android,我不太知道我理解的最后一条语句。

Being a noob to Android, I'm not quite sure I understand the last statement.

话虽这么说,有什么办法来缓存视图状态?如果不是这样,我想我就继续前进,去与留的所有片段的页面在内存中。

That being said, is there any way to cache View state? If not, I think I'll just go ahead and go with leaving all the fragment pages in memory.

推荐答案

我有同样的问题的问题,通过实施这两个功能解决了这个问题。

I had the same problem problem and solved it by implementing these two functions

    public void onSaveInstanceState (Bundle outState)
    public void onActivityCreated (Bundle savedInstanceState)

这是我想保存的片段。在第一个功能,您应在包保存,你需要恢复的意见之日起(以我来说,我有一堆微调,所以我用 一个int数组来保存它们的位置)。第二个功能,恢复您的片段时,这就是所谓的,是您实现恢复过程。

on the fragments that I wanted to save. On the first function, you should save in the Bundle the date that you need to restore the views ( in my case I had a bunch of spinner so I used an int array to save their positions). The second function, which is called when restoring your fragment, is where you implement the restoring process.

我希望这有助于。我还做了我的适配器从FragmentStatePageAdapter继承,但我不知道这是强制性的。

I hope this helps. I also made my adapter to inherit from FragmentStatePageAdapter but I am not sure that this is mandatory.

这篇关于安卓:Viewpager和FragmentStatePageAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆