Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992
Fragmentpageradapter is a new adapter that appears in the ANDROID-SUPPORT-V4 support package, inherited from Pageradapter, and is specifically designed to match the Viewpager that appear in the support package.
Pageradapter in the previous article in a simple introduction, do not remember how to use the first to see.
Fragmentpageradapter, see known, this adapter is used to achieve fragment in the Viewpager inside the sliding switch, so if we want to achieve fragment left and right sliding, You can choose Viewpager and Fragmentpageradapter implementations.
Fragmentpageradapter has its own caching strategy, and when used in conjunction with Viewpager, it caches the current fragment and the left one, the right one, and a total of three fragment objects.
If there are three fragment, then after Viewpager initialization, 3 fragment will be loaded, the middle fragment will only be loaded once throughout the life cycle, when the leftmost fragment is displayed, The rightmost fragment is destroyed because it is out of cache range, and the rightmost fragment is initialized again when it slips into the middle of the fragment.
In the current version, the most suitable for a fixed number of occasions, such as a 3 tab tab fragment sliding interface. Fragmentpageradapter will cache the fragment we've browsed and save the temporary state of these interfaces so that when we swipe left and right, the interface transitions more smoothly. However, this also increases the memory that the program consumes. If your scenario is more fragment, use Fragmentstatepageradapter.
When we use Fragmentpageradapter, its host Viewpager must have an ID.
If you want to use fragmentpageradapter, we need to implement 2 methods, as shown below.
Fragmentpageradapter adapter =New Fragmentpageradapter ( Getsupportfragmentmanager ()) { @Override Publicint GetCount () { return Fragments.size (); } @Override public Fragment getItem (int position) { return Fragments.get (position); } } };
GetCount () returns the number of Viewpager pages, GetItem () returns the Fragent object to display.
In addition to Fragmentpageradapter, there is also a class is specifically implemented Viewpager fragment data adaptation, called Fragmentstatepageradapter.
Fragmentstatepageradapter is a subclass of Pageradapter, and this adapter is very useful for the sliding of multiple fragment interfaces, and it works very much like a ListView. When the fragment is not visible to the user, the entire fragment is destroyed and only the saved state of the fragment is saved. Based on this feature, Fragmentstatepageradapter is more suitable for conversion between many interfaces than Fragmentpageradapter, and consumes less memory resources.
Similarly, the host Viewpager must have an ID.
If we want to use fragmentstatepageradapter, we need to implement 2 methods, GetCount () returns the number of Viewpager pages, GetItem () returns the Fragent object to display. The method of use is exactly the same as Fragmentpageradapter
"Android Interface Implementation" Fragmentpageradapter and Fragmentstatepageradapter use of details and differences