[Android interface implementation] FragmentPagerAdapter and FragmentStatePagerAdapter use details and differences, pageradapter details
Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992
FragmentPagerAdapter is a new adapter in the android-support-v4 support package, inherited from PagerAdapter, is specially used to adapt to the ViewPager in the support package.
PagerAdapter has made a brief introduction in previous articles. If you do not remember how to use it, you can take a look at it first.
FragmentPagerAdapter. This adapter is used to implement sliding switching between Fragment and ViewPager. Therefore, if you want to implement sliding between left and right of Fragment, you can choose ViewPager and FragmentPagerAdapter.
FragmentPagerAdapter has its own cache policy. When used with ViewPager, it caches the current Fragment and one on the left and one on the right. There are three Fragment objects in total.
If there are three Fragment, the three fragment will be loaded after ViewPager initialization, And the Fragment in the middle will be loaded only once throughout the lifecycle, when the leftmost Fragment is displayed, the rightmost Fragment will be destroyed because it is out of the cache range. When it slides to the middle Fragment again, the rightmost Fragment will be reinitialized.
In the current version, it is most suitable for a fixed number of occasions, such as a fragment sliding interface with three tab labels. FragmentPagerAdapter caches the Fragment pages we have browsed and saves the temporary status of these interfaces. This way, when we slide left and right, the interface switches more smoothly. However, this will also increase the memory occupied by the program. If the application scenario is more Fragment, use the FragmentStatePagerAdapter.
When we use the FragmentPagerAdapter, its host ViewPager must have an id.
To use the FragmentPagerAdapter, we need to implement two methods, as shown below.
FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {@Overridepublic int getCount() {return fragments.size();}@Overridepublic Fragment getItem(int position) {return fragments.get(position);}}};
GetCount () returns the number of ViewPager pages, and getItem () returns the fragent object to be displayed.
In addition to the FragmentPagerAdapter, there is also a class specifically designed to adapt the Fragment data of ViewPager, called FragmentStatePagerAdapter.
FragmentStatePagerAdapter is a subclass of PagerAdapter. This adapter is very useful for sliding multiple Fragment interfaces. It works very similar to listview. When Fragment is invisible to users, the entire Fragment will be destroyed and the Fragment will only be 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.
To use the FragmentStatePagerAdapter, we need to implement two methods. getCount () returns the number of ViewPager pages, and getItem () returns the fragent object to be displayed. The usage is exactly the same as that of the FragmentPagerAdapter.
Conflict between v13appFragmentPagerAdapter and appFragment
Then you can import v4.app. FragmentPagerAdapter.
Does android use fragment and other buttons to implement different la s on the interface?
ViewPager, point to end!