The difference between Fragmentpageradapter and Fragmentstatepageradapter

Source: Internet
Author: User

By default (Setoffscreenpagelimit not called)

Fragmentpageradapter: Save all the joined fragment, although the page with a step of more than 1 will call Destroyitem, but in the fragment life cycle, only the Ondestroyview call, No call to Ondestory, no call to Ondetach, so fragment just destroyed the view above, fragment did not destroy, the next time you create, will only invoke Oncreateview and onactivitycreated, So all the fragment in Fragmentpageradapter are not destroyed, which takes up large memory, while avoiding frequent destruction and creation, which is suitable for fewer pages.

Fragmentstatepageradapter: For the fragment within the step size, like Fragmentpageradapter, no destroy operations are invoked, shown again without recreating, Fragment that are outside of the step size will invoke Destroyitem, unlike Fragmentpageradapter, which will actually destroy (both view and fragment, call Ondestroyview, and all subsequent destruction methods). The reconstruction starts from the initial onattach to the onactivitycreated, and applies to more pages.


Combined with adapter's notifydatasetchanged use:

Originally Viewpager fragment order for 1,3,2,5 seconds after the need to become 1,2,3, if the use of Fragmentpageradapter, will always retain the original fragment, Notifydatasetchanged can only change the tab title, Viewpager inside the fragment will not change

Package com.qf.zhouyi.fragmentstatepageradapter;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.support.design.widget.TabLayout;
Import android.support.v4.app.Fragment;
Import Android.support.v4.app.FragmentManager;
Import Android.support.v4.app.FragmentPagerAdapter;
Import Android.support.v4.view.PagerAdapter;
Import Android.support.v4.view.ViewPager;
Import android.support.v7.app.AppCompatActivity;
Import Android.util.Log;

Import Android.view.ViewGroup;
Import java.util.ArrayList;


Import java.util.List;

    public class MyActivity extends appcompatactivity {Handler mhandler;
        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

        Setcontentview (R.layout.activity_main);
        Final list<fragment> listfags = new arraylist<> ();
        Listfags.add (New Fragmentone ());
        Listfags.add (New Fragmentthree ());

        Listfags.add (New Fragmenttwo ()); Final list<string> lsttitles = new arraylist<> ();
        Lsttitles.add ("1");
        Lsttitles.add ("3");

        Lsttitles.add ("2");
        Viewpager vptest = (viewpager) Findviewbyid (R.ID.AC_TAB_VP); Final Pageradapter Adapter =new find_tab_adapter (Getsupportfragmentmanager (), listfags, ls
        Ttitles);



        Vptest.setadapter (adapter);
        Tablayout tablayout = (tablayout) Findviewbyid (r.id.ac_tab_layout);
        Tablayout.settabsfrompageradapter ();

        Tablayout.setupwithviewpager (vptest);
        Mhandler = new Handler (); Mhandler.postdelayed (New Runnable () {@Override public void run () {Listfags.clear
                ();
                Listfags.add (New Fragmentone ());
                Listfags.add (New Fragmenttwo ());


                Listfags.add (New Fragmentthree ());
                Lsttitles.clear ();
                Lsttitles.add ("1");
                Lsttitles.add ("2"); Lsttitles.adD ("3");

            Adapter.notifydatasetchanged ();

    }, 5000); } public class Find_tab_adapter extends Fragmentpageradapter {private list<fragment> list_fragment                         ;                              Fragment list private list<string> list_title; List of tab names public Find_tab_adapter (Fragmentmanager FM, list<fragment> list_fragment, list<string> Li
            St_title) {super (FM);
            This.list_fragment = list_fragment;
        This.list_title = List_title;
        @Override public Fragment getitem (int position) {return list_fragment.get (position);
        @Override public int GetCount () {return list_title.size ();  //This method is used to display the name on the tab @Override public charsequence getpagetitle (int position) {return
        List_title.get (position% list_title.size ()); } @OVerride public void Destroyitem (ViewGroup container, int position, object object) {Super.destroyitem (
            container, Position, object);
        LOG.D ("Zy", "Destroy page:" +position); }//@Override//public int GetItemPosition (object) {//return Pageradapter.position_no
NE;
 //        }
    }
}

If you write the annotation getitemposition, the 5s fragment order becomes 1,3,3

When Pageradapter.notifydatasetchanged () is triggered, viewpager.datasetchanged () can also be triggered. The function will use the return value of getitemposition () to judge, and if it is position_unchanged, do nothing; if position_none, call Pageradapter.destroyitem () To remove the object and set it to need to be refreshed (Needpopulate = true) to trigger the generation of a new object

But the middle one is not destroyed and will not be regenerated.


If the adpter is replaced by fragmentstatepageradapter,getitemposition, the fragment order becomes 1,3,3 after the position_none,5s is returned. GetItemPosition returned to Position_none and became a normal 1,2,3.



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.