Fragment + viewpager Optimization

Source: Internet
Author: User

Fragment + viewpager Optimization

 

 

The last time I wrote a Q & A project, I used the fragment + viewpager architecture. Later I found that after a few strokes, I would drag them back and reload the layout and obtain data again, in this way, the entire program and card occupy too much network resources.

At that time, the solution was to rewrite the view and use the basic Basepageradapder to solve the problem elegantly.

At that time, I knew it was the reason for calling the onCreateView method again, but there was no good solution. Now I know it. here we will record it.

 

I tested viewpager to load four fragment:

The first entry:

 

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
Slide right to the second interface:

 

 

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
Slide 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
Slide right to the fourth page:
03-01 13:56:22.021  24165-24165/com.graypn.modelproject I/onDestroyView2﹕ onDestroyView
Draw the third interface to the left:
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 loads two fragment links to the current page, destroys the view of the third adjacent page, and CALLS oncreateview and onactivityreate again.

We need to optimize our fragment, as shown below:

 

/*** Provides the encapsulated post-base class of fragment, and provides the context to the sub-class using ** @ author Graypn */public abstract class basefrfragment 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, ViewGrou P container, Bundle savedInstanceState) {if (rootView = null) {rootView = initView (inflater);} return rootView;} @ Override public void onActivityCreated (Bundle savedInstanceState) {super. onActivityCreated (savedInstanceState); if (! HasInitData) {initData (); hasInitData = true ;}}@ Override public void onDestroyView () {super. onDestroyView (); (ViewGroup) rootView. getParent ()). removeView (rootView);}/*** sub-class implements initialization View operation */protected abstract View initView (LayoutInflater inflater);/*** sub-class implements initialization data operation (called by the sub-class itself) */public abstract void initData ();/*** encapsulate download data from the Network */protected void loadData (HttpRequest. httpMethod method, String url, RequestParams params, RequestCallBack
 
  
Callback) {if (0 = NetUtils. isNetworkAvailable (getActivity () {new CustomToast (getActivity (), no network, check the network connection !, 0). show () ;}else {NetUtils. loadData (method, url, params, callback );}}}
 

Add rootView to cache the loaded view. If yes, data is not reloaded.

 

Add a variable to identify whether the data has been loaded. If the data has been loaded, the data will not be reloaded.

It is an elegant solution to the optimization problem, hoping to help students with the same problem.

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.