FragmentPagerAdapter和FragmentStatePagerAdapter有什么区别? [英] What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?

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

问题描述

FragmentPagerAdapterFragmentStatePagerAdapter有什么区别?

关于FragmentPagerAdapter Google指南说:

About FragmentPagerAdapter Google's guide says:

此版本的寻呼机最适合在少数情况下使用 通常会有更多的静态片段要分页,例如一组 标签.用户访问的每个页面的片段都将保留在其中 内存,尽管它的视图层次结构在不可见时可能会被破坏. 这可能会导致使用大量内存,因为片段 实例可以保持任意数量的状态.对于更大的套装 的页面,请考虑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个片段.但是它们都是具有大量数据的独立模块.

So I have just 3 fragments. But all of them are separate modules with a large amount of data.

Fragment1处理一些数据(用户输入),并通过活动将其传递到Fragment2中,这只是一个简单的ListFragment. Fragment3也是ListFragment.

Fragment1 handles some data (which users enter) and passes it via activity into Fragment2, which is just a simple ListFragment. Fragment3 is also a ListFragment.

我的问题是:我应该使用哪个适配器? FragmentPagerAdapterFragmentStatePagerAdapter?

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

推荐答案

就像文档所说的那样,请仔细考虑.如果要像书本阅读器这样的应用程序,则不希望立即将所有片段都加载到内存中.您希望在用户读取时加载和销毁Fragments.在这种情况下,您将使用FragmentStatePagerAdapter.如果仅显示3个标签",其中不包含大量数据(例如Bitmaps),则FragmentPagerAdapter可能很适合您.另外,请记住,默认情况下ViewPager会将3个片段加载到内存中.您提到的第一个Adapter可能会破坏View层次结构并在需要时重新加载它,第二个Adapter仅保存Fragment的状态并完全破坏它,如果用户随后返回该页面,则状态被检索.

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天全站免登陆