"Android Interface Implementation" Fragmentpageradapter and Fragmentstatepageradapter use of details and differences

Source: Internet
Author: User

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

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.