Previous Article
FragmentPagerAdapter,
FragmentStatePagerAdapter,
FragmentPagerAdapter,
And
FragmentStatePagerAdapter
These adapters are available in API 4 + support and API
13 + support. As follows: fragment is used to represent a page, which is simpler and more intuitive. Some features provided by fragment allow us to conveniently manage each page, fragmentmanager can be used to find fragment based on the ID or tag,
Dynamic addition, deletion, and replacement allow fragment to manage its own lifecycle. Like activity, fragment provides some lifecycle callback methods.
When fragment becomes a page of viewpager, fragmentmanager keeps saving the created fragment. Even if this page is not displayed, the fragment object will not be destroyed, wait for the display again in the background. However, if fragment is no longer visible, its view level will be destroyed and its view level will be re-created next time it is displayed. Because fragmentpageradapter is used, the fragment object remains in the memory, so when there are a large number of display pages, fragmentpageradapter is not suitable for use, fragmentpageradapter applies to only a few page situations, such as tabs. In this case, you can consider using the fragmentstatepageradapter.
When fragmentstatepageradapter is used
If fragment is not displayed, the fragment object will be destroyed, but the onsaveinstancestate (bundle outstate) method will be called back to save the fragment state before the ondestroy () method is called back, when the next fragment is displayed, the state value of the storage is obtained through oncreate (bundle savedinstancestate). fragmentstatepageradapter is more suitable for scenarios with many pages, such as a page listview.
Note that when using the fragmentpageradapter, you must set a valid ID for its host viewpager.
! The following is an example of using fragmentpageradapter. Note that the destroyitem () method is not used to destroy
The fragment object is destroy and the fragment view.
Class myfragmentpageradapter extends fragmentpageradapter {public myfragmentpageradapter (fragmentmanager FM) {super (FM) ;}@ overridepublic fragment getitem (INT position) {return mypagefragment. create (position) ;}@ overridepublic int getcount () {return mpagernum; // indicates the number of pages} @ overridepublic void destroyitem (viewgroup container, int position, object) {// here destroy is the view level of fragment, not the destroy fragment object super. destroyitem (container, position, object); log. I ("info", "Destroy item... ");}}
The following is a sample fragment program. As mentioned above, when using the fragmentpageradapter, the created fragment will remain in the memory and will not be destroyed, however, its view level is destroyed, so the oncreate () method is called only once, while the oncreateview () method is called every time fragment is invisible to the visible, we can see that fragment has some lifecycle callback methods.
Onpause (), ondestroy (), and so on
Public class mypagefragment extends fragment {public static final string arg_page = "page_num"; // the current page is private int currentpagenum; Public mypagefragment () {} public static mypagefragment create (INT pagernum) {mypagefragment mypageframent = new mypagefragment (); bundle Arg = new bundle (); arg. putint (arg_page, pagernum); mypageframent. setarguments (ARG); Return mypageframent;}/* (non-javadoc) * @ see android. support. v4.app. fragment # oncreate (Android. OS. bundle) ** call when fragment is created **/@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); log. I ("info", "oncreate .. "); currentpagenum = getarguments (). getint (arg_page);}/* (non-javadoc) * @ see android. support. v4.app. fragment # oncreateview (Android. view. layoutinflater, android. view. viewgroup, android. OS. bundle) ** call when fragment is invisible to visible **/@ overridepublic view oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate) {log. I ("info", "oncreateview .. "); viewgroup rootview = (viewgroup) Inflater. inflate (R. layout. per_pager1, container, false); Switch (currentpagenum) {Case 0: rootview. setbackgroundresource (R. drawable. pagedomainbg); break; Case 1: rootview. setbackgroundresource (R. drawable. page2_bg); break; Case 2: rootview. setbackgroundresource (R. drawable. page3_bg); break; default: break;} return rootview;} @ overridepublic void onpause () {super. onpause () ;}@ overridepublic void ondestroy () {super. ondestroy (); log. I ("info", "myfragment destroy... ");}}
The use of viewpager is the same as that described in the previous article. I will not introduce it here. The use of fragmentpageradapter is quite simple. The key is to understand its meaning and use cases ~