Android Viewpager fragment boost performance with lazy loading

Source: Internet
Author: User

??

Android Viewpager fragment boost performance with lazy loading

Fragment is becoming more prevalent in today's Android development, but when Viewpager is combined with fragment, due to the inherent loading mechanism of Android Viewpager, a more serious load-performance problem, specifically, Assuming that there are more than n fragment in a Viewpager, then Viewpager will initialize the Fragmentpageradapter at least 3 fragment (if fragment more than 3) at one time during the initialization phase. Creating and Loading Fragmentpageradapter
View and data in fragment. This problem is not common in the previous lightweight apps, and today's apps, the volume of data is getting bigger and the view is getting more and more cool, which means that if all of these fragment are created at once, it will result in a more serious performance overhead and poor handling will cause the app to crash when the memory stack overflows.
One solution to this problem is to use fragment's setuservisiblehint.
Setuservisiblehint's official Android Documentation:

Setuservisiblehintvoid Setuservisiblehint (Boolean isvisibletouser) Set a hint to the system about whether this fragment ' s UI is currently visible to the user. This hint defaults to true and are persistent across fragment instance state save and restore.  An app could set this to false to indicate that the fragment's UI is scrolled out of visibility or was otherwise not directly Visible to the user. This is used by the system to prioritize operations such as fragment lifecycle updates or loader ordering behavior. Note:this method may be called outside of the fragment lifecycle. And thus have no ordering guarantees with regard to fragment lifecycle method calls. Parametersisvisibletouserboolean:true if this fragment ' s UI was currently visible to the user (default), False if it is no T.

I wrote an example that illustrates the use of Viewpager in Android to load fragment using lazy-loaded instance code:

Package Zhangphil.app;import Android.os.handler;import Android.os.message;import android.os.systemclock;import Android.support.annotation.nullable;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmentpageradapter;import Android.support.v4.view.viewpager;import Android.support.v7.app.appcompatactivity;import Android.os.Bundle; Import Android.util.log;import android.view.layoutinflater;import android.view.view;import android.view.ViewGroup; Import Android.widget.textview;public class Mainactivity extends Appcompatactivity {@Override protected void Oncrea        Te (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Viewpager pager = (Viewpager) Findviewbyid (R.id.viewpager);        Itemfragmentpageradapter adapter = new Itemfragmentpageradapter (This.getsupportfragmentmanager ());    Pager.setadapter (adapter); } public static Class ItemfragmeNT extends Fragment {private TextView text; @Override public void Setuservisiblehint (Boolean isvisibletouser) {LOG.D ("is the current fragment visible?            ", String.valueof (Isvisibletouser));            if (isvisibletouser) {//Load loaddata ();            } else {}}//Here it is assumed that a fragment need to load a lot of data is complicated and time consuming to get enough data to render view private void LoadData () { Final Handler Handler = new Handler () {@Override public void Handlemessage (MESSAG E msg) {if (Msg.what = = 0xa1) {//load complete, assignment Text.settext ("                    Fragment lazy loading [email protected] ");            }                }            };                    New Thread (New Runnable () {@Override public void run () {//) assuming this is a time-consuming operation                    Systemclock.sleep (10000);                Handler.sendemptymessage (0XA1);      }      }). Start (); } @Override public void onviewcreated (view view, @Nullable Bundle savedinstancestate) {text = (T Extview) View.findviewbyid (Android.            R.ID.TEXT1);        Text.settext ("Loading ..."); } @Nullable @Override public View oncreateview (Layoutinflater inflater, @Nullable viewgroup container , @Nullable Bundle savedinstancestate) {return Inflater.inflate (Android.        R.layout.simple_list_item_1, NULL); }} Private class Itemfragmentpageradapter extends Fragmentpageradapter {public Itemfragmentpageradapter (Fra        Gmentmanager FM) {super (FM);        } @Override public Fragment getItem (int position) {return new itemfragment ();        } @Override public int getcount () {return 999; }    }}


Android Viewpager fragment boost performance with lazy loading

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.