FragmentPagerAdapter和FragmentStatePagerAdapter有什麼區別?

本文翻譯自:What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter ? FragmentPagerAdapterFragmentStatePagerAdapter什麼區別?

About FragmentPagerAdapter Google's guide says: 關於FragmentPagerAdapter Google的指南說:

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

And about FragmentStatePagerAdapter : 關於FragmentStatePagerAdapter

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. FragmentPagerAdapter相比,這允許尋呼機保持與每個被訪問頁面相關聯的更少的存儲器,代價是在頁面之間切換時可能具有更多的開銷。

So I have just 3 fragments. 所以我只有3個片段。 But all of them are separate modules with a large amount of data. 但它們都是具有大量數據的獨立模塊。

Fragment1 handles some data (which users enter) and passes it via activity into Fragment2 , which is just a simple ListFragment . Fragment1處理一些數據(用戶輸入)並通過活動將其傳遞到Fragment2 ,這只是一個簡單的ListFragment Fragment3 is also a ListFragment . Fragment3也是一個ListFragment

So my questions are : Which adapter should I use? 所以我的問題是 :我應該使用哪個適配器? FragmentPagerAdapter or FragmentStatePagerAdapter ? FragmentPagerAdapter還是FragmentStatePagerAdapter


#1樓

參考:https://stackoom.com/question/1GfCh/FragmentPagerAdapter和FragmentStatePagerAdapter有什麼區別


#2樓

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. 您希望在用戶閱讀時加載和銷燬Fragments In this case you will use FragmentStatePagerAdapter . 在這種情況下,您將使用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. 如果您只是顯示3個不包含大量重量數據的“標籤”(如Bitmaps ),那麼FragmentPagerAdapter可能很適合您。 Also, keep in mind that ViewPager by default will load 3 fragments into memory. 另外,請記住, ViewPager默認會將3個片段加載到內存中。 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. 您提到的第一個Adapter可能會破壞View層次結構並在需要時重新加載它,第二個Adapter僅保存Fragment的狀態並完全銷燬它,如果用戶然後返回該頁面,則檢索狀態。


#3樓

  • FragmentPagerAdapter stores the whole fragment in memory, and could increase a memory overhead if a large amount of fragments are used in ViewPager . FragmentPagerAdapter將整個片段存儲在內存中,如果在ViewPager中使用了大量片段,則會增加內存開銷。

  • In contrary its sibling, FragmentStatePagerAdapter only stores the savedInstanceState of fragments, and destroys all the fragments when they lose focus. 與其兄弟相反, FragmentStatePagerAdapter僅存儲片段的savedInstanceState,並在失去焦點時銷燬所有片段。

  • Therefore FragmentStatePagerAdapter should be used when we have to use dynamic fragments, like fragments with widgets, as their data could be stored in the savedInstanceState .Also it wont affect the performance even if there are large number of fragments. 因此,當我們必須使用動態片段(如具有小部件的片段)時,應使用FragmentStatePagerAdapter ,因爲它們的數據可以存儲在savedInstanceState 。即使存在大量片段,它也不會影響性能。

  • In contrary its sibling FragmentPagerAdapter should be used when we need to store the whole fragment in memory. 相反,當我們需要將整個片段存儲在內存中時,應該使用它的兄弟FragmentPagerAdapter

  • When I say the whole fragment is kept in memory it means, its instances wont be destroyed and would create a memory overhead. 當我說整個片段保存在內存中意味着,它的實例不會被破壞並且會產生內存開銷。 Therefore it is advised to use FragmentPagerAdapter only when there are low number of fragments for ViewPager . 因此,建議僅在ViewPager的片段數較少時才使用FragmentPagerAdapter

  • It would be even better if the fragments are static, since they would not be having large amount of objects whose instances would be stored. 如果片段是靜態的,那將會更好,因爲它們不會有大量的實例存儲的對象。

To be more detail, 更詳細一點,

FragmentStatePagerAdapter: FragmentStatePagerAdapter:

  • with FragmentStatePagerAdapter ,your unneeded fragment is destroyed.A transaction is committed to completely remove the fragment from your activity's FragmentManager . 使用FragmentStatePagerAdapter ,您的不需要的片段將被銷燬。事務將被提交以從您的活動的FragmentManager完全刪除該片段。

  • The state in FragmentStatePagerAdapter comes from the fact that it will save out your fragment's Bundle from savedInstanceState when it is destroyed.When the user navigates back,the new fragment will be restored using the fragment's state. FragmentStatePagerAdapter的狀態來自這樣一個事實:當它被銷燬時,它會從savedInstanceState保存你的片段的Bundle 。當用戶導航回來時,將使用片段的狀態恢復新的片段。

FragmentPagerAdapter: FragmentPagerAdapter:

  • By comparision FragmentPagerAdapter does nothing of the kind.When the fragment is no longer needed. 通過比較FragmentPagerAdapter什麼都不做。當不再需要片段時。 FragmentPagerAdapter calls detach(Fragment) on the transaction instead of remove(Fragment) . FragmentPagerAdapter調用事務上的detach(Fragment)而不是remove(Fragment)

  • This destroy's the fragment's view but leaves the fragment's instance alive in the FragmentManager .so the fragments created in the FragmentPagerAdapter are never destroyed. 這種破壞是片段的觀點,但離開片段的實例活着的FragmentManager 。所以在創建的片段FragmentPagerAdapter永遠不會被銷燬。


#4樓

FragmentPagerAdapter存儲從適配器獲取的先前數據,而FragmentStatePagerAdapter每次執行時都從適配器獲取新值。


#5樓

Something that is not explicitly said in the documentation or in the answers on this page (even though implied by @Naruto), is that FragmentPagerAdapter will not update the Fragments if the data in the Fragment changes because it keeps the Fragment in memory. 文檔或本頁答案中未明確說明的內容(即使@Naruto暗示),如果Fragment中的數據發生更改, FragmentPagerAdapter將不會更新Fragments,因爲它會將Fragment保留在內存中。

So even if you have a limited number of Fragments to display, if you want to be able to refresh your fragments (say for example you re-run the query to update the listView in the Fragment), you need to use FragmentStatePagerAdapter. 因此,即使您要顯示有限數量的片段,如果您希望能夠刷新片段(例如,重新運行查詢以更新片段中的listView),則需要使用FragmentStatePagerAdapter。

My whole point here is that the number of Fragments and whether or not they are similar is not always the key aspect to consider. 我的全部觀點是碎片的數量以及它們是否相似並不總是需要考慮的關鍵方面。 Whether or not your fragments are dynamic is also key. 您的碎片是否是動態的也是關鍵。


#6樓

FragmentStatePagerAdapter = To accommodate a large number of fragments in ViewPager. FragmentStatePagerAdapter =在ViewPager中容納大量片段。 As this adapter destroys the fragment when it is not visible to the user and only savedInstanceState of the fragment is kept for further use. 由於此適配器在用戶不可見時銷燬該片段,並且僅保留片段的savedInstanceState以供進一步使用。 This way a low amount of memory is used and a better performance is delivered in case of dynamic fragments. 這樣,使用少量內存,並且在動態片段的情況下提供更好的性能。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章