Reprint please join the link, thank you: http://blog.csdn.net/u013173289/article/details/44002371
Last time I wrote a question and answer project, using the Fragment+viewpager architecture, later found that after a few strokes, and then again, will reload the layout, retrieve the data, so the whole program and card, and take up too much network resources.
The solution was to rewrite the view, with the most basic basepageradapder, is not very elegant to solve the problem.
At that time know is to recall the Oncreateview method reason, but there is no good solution, now know, here to record.
I experimented with Viewpager loading four fragment:
The first time you enter:
<span style= "FONT-SIZE:18PX;" >03-01 13:50:16.151 22667-22667/com.graypn.modelproject i/oncreate1:oncreate03-01 13:50:16.151 22667-22667/com.graypn.modelproject i/oncreateview1:oncreateview03-01 13:50:16.161 22667-22667/ Com.graypn.modelproject i/onactivitycreated1:onactivitycreated03-01 13:50:16.161 22667-22667/ Com.graypn.modelproject i/oncreate2:oncreate03-01 13:50:16.161 22667-22667/com.graypn.modelproject i/ oncreateview2:oncreateview03-01 13:50:16.161 22667-22667/com.graypn.modelproject i/onactivitycreated2: Onactivitycreated</span>
swipe right to the second interface:
<span style= "FONT-SIZE:18PX;" >03-01 13:51:22.391 22667-22667/com.graypn.modelproject i/oncreate3:oncreate03-01 13:51:22.391 22667-22667/com.graypn.modelproject i/oncreateview3:oncreateview03-01 13:51:22.401 22667-22667/ Com.graypn.modelproject i/onactivitycreated3:onactivitycreated</span>
swipe right to the third interface:
03-01 13:55:24.351 24165-24165/com.graypn.modelproject i/ondestroyview1:ondestroyview03-01 13:55:24.351 24165-24165/com.graypn.modelproject i/oncreate4:oncreate03-01 13:55:24.351 24165-24165/ Com.graypn.modelproject i/oncreateview4:oncreateview03-01 13:55:24.361 24165-24165/com.graypn.modelproject I/ onactivitycreated4:onactivitycreated
Swipe right to the fourth interface:
03-01 13:56:22.021 24165-24165/com.graypn.modelproject I/ondestroyview2:ondestroyview
Swipe left to the third interface:
03-01 13:58:09.541 24165-24165/com.graypn.modelproject i/oncreateview2:oncreateview03-01 13:58:09.541 24165-24165/com.graypn.modelproject i/onactivitycreated2:onactivitycreated
conclusion: Viewpager will load two fragment connected to the current page, destroying the view of the next third page, re-oncreateview and onactivityreate.
We need to optimize our own fragment, as follows when I refine the class:
<span style= "FONT-SIZE:18PX;" >/** * Provides a Fragment-encapsulated base class that provides context for subclasses using * * @author GRAYPN */public abstract class Basefragment extends Fragment {/ /root view private view Rootview; protected context context; Private Boolean Hasinitdata = false; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); context = Getactivity (); } @Override Public View oncreateview (layoutinflater inflater, ViewGroup container, Bundl E savedinstancestate) {if (Rootview = = null) {Rootview = Initview (Inflater); } return Rootview; } @Override public void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstances Tate); if (!hasinitdata) {initdata (); Hasinitdata = true; }} @Override public void Ondestroyview () {Super.ondestroyview (); ((ViewGroup) rootview.getparent ()). REmoveview (Rootview); The/** * Subclass implements the Initialize view operation */protected abstract View Initview (Layoutinflater inflater); /** * Subclass implements initialization data operation (subclass calls itself) */public abstract void InitData (); /** * Package Downloads data from Network */protected void LoadData (Httprequest.httpmethod method, String URL, Requestparams params, requestcallback<string> callback) {if (0 = = netutils.isnetworkavailable (getactivity ( )) {New Customtoast (getactivity (), "No network, check your network connection! ", 0). Show (); } else {Netutils.loaddata (method, URL, params, callback); }}}</span>
add Rootview, cache the view after loading, and do not reload the data if there is one.
Adds a flag variable that determines whether the data has been loaded, and does not reload the data if the data has already been loaded.
It is elegant to solve the problem of optimization, hoping to help students with the same problem.
About the optimization of Fragment+viewpager