Lazy Loading of Fragment+viewpager

Source: Internet
Author: User

The company app uses Viewpager to load fragment: Each Viewpager page is a fragment,fragment content from the Web, and the function to be implemented is when switching between different pages of Viewpager, can display the page that has been loaded smoothly.

The implementation of the method is very simple answer. Because Viewpager has the ability to cache.

Mviewpager.setoffscreenpagelimit (6);

You can do this by simply adding this sentence: for fragment that have already been loaded, any event of that fragment will not be triggered when it is switched. It can be displayed normally when switching back.

It is important to note that the function of this sentence is to set the number of pre-cached fragment (or other view). If the set number is too large, the fragment that is not displayed when you load the current fragment will cause the current page to load too slowly. So this number should choose the appropriate value.

More Content Blog Links: http://blog.csdn.net/zhouzme/article/details/19707677

  1. Package com.cosmosource.mbi.fragment;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Import Cn.jiabeis.andr.log.CommonLog;
  5. Import Cn.jiabeis.andr.log.LogFactory;
  6. Import com.cosmosource.mbi.MainActivity;
  7. Import COM.COSMOSOURCE.MBI.R;
  8. Import Com.cosmosource.mbi.adapter.TabPagerAdapter;
  9. Import Com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
  10. Import Android.os.Bundle;
  11. Import Android.os.Handler;
  12. Import android.support.v4.app.Fragment;
  13. Import Android.support.v4.view.PagerAdapter;
  14. Import Android.support.v4.view.ViewPager;
  15. Import Android.support.v4.view.ViewPager.OnPageChangeListener;
  16. Import Android.view.LayoutInflater;
  17. Import Android.view.View;
  18. Import Android.view.ViewGroup;
  19. Import android.view.ViewParent;
  20. /**
  21. * @description Viewerpager's Fragment
  22. * @author Herb
  23. * @version 0.1
  24. * @since 2013-11-18 10:29:37
  25. */
  26. public class Viewpagerfragment extends commonfragment{
  27. Private static final Commonlog log = Logfactory.createlog ();
  28. /**
  29. * References to Viewpager objects
  30. */
  31. Public Viewpager Mviewpager;
  32. /**
  33. * Loading fragment containers, each of our interfaces is a fragment
  34. */
  35. Private list<fragment> mfragmentlist = new arraylist<fragment> ();
  36. /**
  37. * Used to record fragment that have been browsed, no longer loading from the Web, because the Viewpager cache function when switching fragemnt
  38. * Does not trigger any events for fragment. Can be saved in full.
  39. * JKX
  40. */
  41. Private list<fragment> browsedfragmentlist = new arraylist<fragment> ();
  42. Private Tabpageradapter TPA = null;
  43. Public list<fragment> getmfragmentlist () {
  44. return mfragmentlist;
  45. }
  46. public void Setmfragmentlist (list<fragment> mfragmentlist) {
  47. This.mfragmentlist = mfragmentlist;
  48. }
  49. @Override
  50. public void OnCreate (Bundle savedinstancestate) {
  51. Super.oncreate (savedinstancestate);
  52. }
  53. @Override
  54. Public View Oncreateview (layoutinflater inflater, ViewGroup container,
  55. Bundle savedinstancestate) {
  56. if (mview==null) {
  57. MView = inflater.inflate (r.layout.frame_viewpager_layout, NULL);
  58. Mviewpager = (Viewpager) Mview.findviewbyid (R.id.viewpager);
  59. Setting up Monitoring
  60. Mviewpager.setonpagechangelistener (New Mysetonpagechangelistener ());
  61. Cache multiple sub-pages, pre-cache 16 menu items
  62. Mviewpager.setoffscreenpagelimit (15);
  63. /* Resolution: Java.lang.IllegalStateException:Recursive entry to executependingtransaction*/
  64. New Handler (). postdelayed (New Runnable () {
  65. @Override
  66. public void Run () {
  67. TPA = new Tabpageradapter (Getfragmentmanager (), mfragmentlist);
  68. Mviewpager.setadapter (TPA);
  69. Mviewpager.setcurrentitem (0);
  70. /* Load url*/here
  71. if (null! = Mfragmentlist && mfragmentlist.size () > 0) {
  72. There are multiple secondary navigation, the default display of the No. 0 navigation corresponding to the fragment
  73. Fragment ff = mfragmentlist.get (0);
  74. if (null! = FF && ff instanceof webviewfragment) {
  75. ((webviewfragment) FF). Cusloadurl ();
  76. }
  77. }
  78. }
  79. }, 10);
  80. }else{
  81. System.out.println ("Viewpagerfragment:mview is not empty, re-use");
  82. }
  83. return mView;
  84. }
  85. @Override
  86. public void onactivitycreated (Bundle savedinstancestate) {
  87. Super.onactivitycreated (savedinstancestate);
  88. Setupviews ();
  89. }
  90. private void Setupviews () {
  91. }
  92. @Override
  93. public void OnDestroy () {
  94. Super.ondestroy ();
  95. }
  96. @Override
  97. public void Ondestroyview () {
  98. TODO auto-generated Method Stub
  99. Super.ondestroyview ();
  100. if (Null!=mview) {
  101. ((ViewGroup) mview.getparent ()). Removeview (MView);
  102. }
  103. }
  104. /**
  105. * Viewpager page tab Toggle Listener
  106. */
  107. Class Mysetonpagechangelistener implements Onpagechangelistener
  108. {
  109. public void onpagescrollstatechanged (int arg0) {
  110. }
  111. public void onpagescrolled (int arg0, float arg1, int arg2) {
  112. }
  113. public void onpageselected (int position) {
  114. Fragment ff = mfragmentlist.get (position);
  115. /**
  116. * determine if it has been browsed, and if not, reload it from the web.
  117. * JKX
  118. */
  119. if (!isbrowsered (FF)) {
  120. if (null! = FF && ff instanceof webviewfragment) {
  121. ((webviewfragment) FF). Cusloadurl ();
  122. }
  123. Browsedfragmentlist.add (FF);
  124. System.out.println ("Number of pages loaded:" +browsedfragmentlist.size ());
  125. }
  126. Mainactivity ma = (mainactivity) getactivity ();
  127. if (null! = MA) {
  128. Ma.getsupportactionbar (). Setselectednavigationitem (position);
  129. int last = Mviewpager.getchildcount ()-1;
  130. if (position = = Last | | position = = 0) {
  131. Ma.getslidingmenu (). Settouchmodeabove (Slidingmenu.touchmode_margin);
  132. } else {
  133. Ma.getslidingmenu (). Settouchmodeabove (Slidingmenu.touchmode_none);
  134. }
  135. }
  136. }
  137. }
  138. /**
  139. * Determine if the fragment has been browsed.
  140. * @param fragment
  141. * @return
  142. * JKX
  143. */
  144. public boolean isbrowsered (Fragment Fragment) {
  145. if (Browsedfragmentlist.contains (fragment)) {
  146. return true;
  147. }else{
  148. return false;
  149. }
  150. }
  151. }

Lazy Loading of Fragment+viewpager

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.