Fragment Viewpager Adapter

Source: Internet
Author: User

http://wangxinghe.me/blog/2015-02-02/android-FragmentPagerAdapter-FragmentStatePagerAdapter/

First, Fragmentpageradapter

Suitable for Fragment a very limited number of cases. When a page is not visible, the view for that page may be destroyed, but all fragment will remain in memory.

If the fragment need to save a lot of state, it will lead to large memory consumption, so for a large number of fragment, we recommend the use of Fragmentstatepageradapter.
When you use Fragmentpageradapter, ViewPager you must have a valid ID.

@Overridepublic Object Instantiateitem (viewgroup container, int position) {if (mcurtransaction = = null) {Mcurt    Ransaction = Mfragmentmanager.begintransaction ();    } final Long itemId = Getitemid (position);    Do we already has this fragment?    String name = Makefragmentname (Container.getid (), itemId);    Fragment Fragment = Mfragmentmanager.findfragmentbytag (name);        if (fragment! = null) {if (DEBUG) log.v (TAG, "Attaching Item #" + ItemId + ": f=" + fragment);    Mcurtransaction.attach (fragment);        } else {fragment = GetItem (position);        if (DEBUG) log.v (TAG, "Adding Item #" + ItemId + ": f=" + fragment);    Mcurtransaction.add (Container.getid (), Fragment, Makefragmentname (Container.getid (), itemId));        } if (Fragment! = Mcurrentprimaryitem) {fragment.setmenuvisibility (false);    Fragment.setuservisiblehint (FALSE); } return fragment;} @Overridepublic void Destroyitem (ViewGroup container, int position, Object object) {if (mcurtransaction = = null) {mcurtransaction = Mfragmentmanager.begintransaction (); } if (DEBUG) log.v (TAG, "Detaching Item #" + getitemid (position) + ": f=" + object + "v=" + ((Fragment) obje    CT). GetView ()); Mcurtransaction.detach ((Fragment) object);} private static String makefragmentname (int viewId, long id) {return "Android:switcher:" + viewId + ":" + ID;}

  

Through the source code, you can see Fragmentpageradapter fill each page of the process:
(1) According to Containerid and position get fragment tag
(2) Fragmentmanager according to the tag to find the corresponding fragment, if there is a direct attach
(3) If it does not exist, call GetItem (position) and set tag to call the Add method.

Conclusion:
(1) The tag of fragment in Fragmentpageradapter is "Android:switcher:" + viewId + ":" + ID;
(2) GetItem (position) in new Fragment (), and do not need to set tag,fragment.instantiate () to

Second, Fragmentstatepageradapter

Suitable for a large number of fragment cases. When the page is not visible, the corresponding fragment instance may be destroyed, but the fragment state will be saved.

As a result, each fragment consumes less memory, but page switching can cause significant overhead.
When using Fragmentstatepageradapter, Viewpager must have a valid ID.

@Overridepublic Object Instantiateitem (viewgroup container, int position) {//If We already has this item instantiate  D, there is nothing//to do.    This can happen when we is restoring the entire pager//from its saved state, where the fragment manager has already    Taken care of restoring the fragments we previously had instantiated.        if (mfragments.size () > position) {Fragment F = mfragments.get (position);        if (f! = null) {return F;    }} if (mcurtransaction = = null) {mcurtransaction = Mfragmentmanager.begintransaction ();    } Fragment Fragment = GetItem (position);    if (DEBUG) log.v (TAG, "Adding Item #" + Position + ": f=" + fragment);        if (msavedstate.size () > position) {fragment.savedstate FSS = msavedstate.get (position);        if (FSS! = null) {fragment.setinitialsavedstate (FSS);    }} while (Mfragments.size () <= position) {mfragments.add (null); } fragment.Setmenuvisibility (FALSE);    Fragment.setuservisiblehint (FALSE);    Mfragments.set (position, fragment);    Mcurtransaction.add (Container.getid (), fragment); return fragment;} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {Fragment Fragment = (Fragment) obje    Ct    if (mcurtransaction = = null) {mcurtransaction = Mfragmentmanager.begintransaction (); } if (DEBUG) log.v (TAG, "Removing Item #" + Position + ": f=" + object + "v=" + ((Fragment) object). GetView (    ));    while (Msavedstate.size () <= position) {msavedstate.add (null);    } msavedstate.set (position, mfragmentmanager.savefragmentinstancestate (fragment));    Mfragments.set (position, NULL); Mcurtransaction.remove (fragment);}

  

According to the source code, Fragmentstatepageradapter fills the flow of each page:
(1) First try to find mfragments list in the index for position whether there is fragment, if there is a direct return;
(2) If not, call GetItem (position) to instantiate the fragment and set the initial state, call the Add
Destruction Process:
(1) Save the Fragment corresponding state (2) Remove Fragment

Conclusion:
(1) Fragment tag with no specified format
(2) Fragmentstatepageradapter will maintain and restore the status of fragment

Fragment Viewpager Adapter

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.