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

问题描述

FragmentPagerAdapterFragmentStatePagerAdapter有什么区别?

关于FragmentPagerAdapter Google指南说:

About FragmentPagerAdapter Google's guide says:

关于FragmentStatePagerAdapter:

所以我只有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有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 18:51