FragmentPagerAdapter和FragmentStatePagerAdapter的区别 [英] Difference between FragmentPagerAdapter and FragmentStatePagerAdapter

本文介绍了FragmentPagerAdapter和FragmentStatePagerAdapter的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间的区别是什么 FragmentPagerAdapter FragmentStatePagerAdapter

关于 FragmentPagerAdapter 谷歌的导游说:

该版本的寻呼机是最适合使用时,有极少数的   通常多个静态片段通过被寻呼,例如一组   标签。该片段的每个页面的用户访问将被保存在   内存,虽然它的视图层次,可以在不可见的破坏。   这可导致使用显著量的内存,因为片段   实例可以坚持到状态的任意金额。对于较大的集   页面,考虑FragmentStatePagerAdapter。

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state. For larger sets of pages, consider FragmentStatePagerAdapter.

和有关 FragmentStatePagerAdapter

当有大量这个版本的寻呼机的是更有用   页面,变得更像一个列表视图。当页面是不可见的   用户,他们的整个片段可能被破坏,只保留   该片段的保存状态。这使得寻呼机扶住多   少与每个访问的网页相比相关联的存储器   FragmentPagerAdapter在潜在的更多的开销,当成本   页之间切换。

This version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.

所以,我刚刚3个片段。但所有这些是与大量数据的一个单独的模块。 片段1 处理一些数据(用户输入),并通过活动传递到 Fragment2 ,这只是简单的 ListFragment Fragment3 也是 ListFragment

So I have just 3 fragments. But all of them is a separate module with large amount of data. Fragment1 handle some data (which user enter) and via activity pass it into Fragment2, which is just simple ListFragment. Fragment3 is also ListFragment.

所以,我的问题是:我应该使用哪种接口? FragmentPagerAdapter FragmentStatePagerAdapter

So my questions are: Which adapter should I use? FragmentPagerAdapter or FragmentStatePagerAdapter?

我将不胜AP preciate对您有所帮助。亚历克斯。附:对不起,我的英语:)

I would greatly appreciate for your help. Alex. P.S. Sorry for my English:)

推荐答案

像文档说,想想这样。如果你做的像一个电子书阅读器的应用程序,你会不会想所有的片段加载到内存中一次。你想加载和销毁片段作为用户读取。在这种情况下,你会使用 FragmentStatePagerAdapter 。如果你只显示3标签不包含大量繁重的数据(如位图),那么 FragmentPagerAdapter 可能适合你。另外,请记住, ViewPager 默认情况下将加载3个片段到内存中。第一个适配器你提到可能会破坏查看的层次结构,并重新在需要时加载,第二个适配器只保存片段的状态,完全破坏它,如果用户再回来到该网页,状态检索。

Like the docs say, think about it this way. If you were to do an application like a book reader, you will not want to load all the fragments into memory at once. You would like to load and destroy Fragments as the user reads. In this case you will use FragmentStatePagerAdapter. If you are just displaying 3 "tabs" that do not contain a lot of heavy data (like Bitmaps), then FragmentPagerAdapter might suit you well. Also, keep in mind that ViewPager by default will load 3 fragments into memory. The first Adapter you mention might destroy View hierarchy and re load it when needed, the second Adapter only saves the state of the Fragment and completely destroys it, if the user then comes back to that page, the state is retrieved.

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

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