Implementation of page delay loading in Android Viewpager

Source: Internet
Author: User

For the use of Viewpager, if the requested data on the memory consumption is not, this is ideal, but if the requested data on memory consumption is relatively large, so easy to appear oom (such as pictures), When using Viewpager, using setoffscreenpagelimit (int size) is the default of 1, so how do we set this value, when loading the data will be the current page and the next page of the data at the same time the request, This in some cases is very performance-affecting, there is the current page and the next page in the Viewpager count as the same page, because you are not able to control data through the life cycle of delayed loading, So now many users modify the V4 package to modifythe default value of setoffscreenpagelimit (int size) is 0, but although the effect is implemented, but will cause another problem, that is, when you switch the page, the middle transition is a big black screen, Mygod This kind of modification to the experience is a deadly blow for me, but finally I finally got the fix;

Before a way in my previous blog has written, the following is another way: combined with fragment to deal with, nonsense does not say directly on the code:

mainactivity
Public
classMainactivityextendsfragmentactivity {PrivateViewpager Pager; PrivateArraylist<fragment> FTS =NewArraylist<fragment>(); Privatestring[] URLs =Newstring[]{"http://t2.qpic.cn/mblogpic/d5149f8549e394000aa6/2000", "http://t2.qpic.cn/mblogpic/fad173ab259d7efbfecc/2000", "http://t2.qpic.cn/mblogpic/463b55016d51d4b9c172/2000", "http://t2.qpic.cn/mblogpic/ab3f96d6095ec82e6d40/2000", "http://t2.qpic.cn/mblogpic/e61f017bcad2684e79f2/2000", "http://t2.qpic.cn/mblogpic/d271142c68e6e1d5b8de/2000", "http://t2.qpic.cn/mblogpic/ef50986bdf4dd8801060/2000", "http://t2.qpic.cn/mblogpic/71585d34aaa60b9dcc2a/2000", "http://t2.qpic.cn/mblogpic/e0011bad45ae23889216/2000", "http://t2.qpic.cn/mblogpic/6e84cb60bf0e6b4945f6/2000", "http://t2.qpic.cn/mblogpic/5a9217624c130c2fa9f8/2000", "http://t2.qpic.cn/mblogpic/7ddb0e9618754fc4bc8e/2000", "http://t2.qpic.cn/mblogpic/eed45f9d4b5001a04b30/2000", "http://t2.qpic.cn/mblogpic/62353a0dbdd40bc1be9a/2000", "http://t2.qpic.cn/mblogpic/0ccf7f16318331e9a892/2000", "http://t2.qpic.cn/mblogpic/4e55efffef93f6d4a86e/2000", "http://t2.qpic.cn/mblogpic/4ac76f1427e4278b7172/2000", "http://t2.qpic.cn/mblogpic/0db3a5a1047d7ee52bdc/2000", "http://t2.qpic.cn/mblogpic/c65d09035d7b6c644d6a/2000", "http://t2.qpic.cn/mblogpic/7ebf4b49bf7e2ef6a716/2000", "http://t2.qpic.cn/mblogpic/4ba5fd867149273f9b02/2000", "http://t2.qpic.cn/mblogpic/4f375f19fbd08151d0f8/2000", "http://t2.qpic.cn/mblogpic/1c22a0574274d0e3c866/2000", "http://t2.qpic.cn/mblogpic/50cdf4f63120ccf74e86/2000", "http://t2.qpic.cn/mblogpic/ab8588008e7865927d7c/2000", "http://t2.qpic.cn/mblogpic/1e9e65d61d5f2bb8ebc4/2000", "http://t2.qpic.cn/mblogpic/6913e7ad5c7267969d9a/2000", "http://t2.qpic.cn/mblogpic/54157580cc7ddf7b2870/2000", "http://t2.qpic.cn/mblogpic/0bd2f9be53d2c9aec8ca/2000", "http://t2.qpic.cn/mblogpic/2041a03d63cc6c5b9b92/2000", }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Pager=(Viewpager) Findviewbyid (R.id.pager); for(inti = 0; i < urls.length; i++) {Fts.add (Photofragment.instance (urls[i], i)); } pager.setadapter (NewMypageradapter (Getsupportfragmentmanager (), FTS)); Pager.setoffscreenpagelimit (Fts.size ()); Pager.setcurrentitem (0); } Private classMypageradapterextendsfragmentpageradapter{PrivateArraylist<fragment>list; PublicMypageradapter (Fragmentmanager fm,arraylist<fragment>list) { Super(FM); This. List =list; } @Override PublicFragment GetItem (intarg0) { returnList.get (arg0); } @Override Public intGetCount () {returnlist.size (); } }}

Photofragment:

 PackageCom.zhizhi.viewpagernocache;ImportCom.nostra13.universalimageloader.core.ImageLoader;ImportAndroid.os.Bundle;Importandroid.support.annotation.Nullable;Importandroid.support.v4.app.Fragment;ImportAndroid.util.Log;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ImageView; Public classPhotofragmentextendsFragment {Private Final StaticString TAG = photofragment.class. GetName (); PrivateString URL; Private intindex; PrivateImageView image;  Public Staticphotofragment instance (String URL,intindex) {Photofragment Fragment=Newphotofragment (); Bundle Bundle=NewBundle (); Bundle.putstring ("url", URL); Bundle.putint ("Index", index);        Fragment.setarguments (bundle); returnfragment; } @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); if(Getarguments ()! =NULL) {URL= Getarguments (). getString ("url"); Index= Getarguments (). GetInt ("index")); } setretaininstance (true); } @Override Public voidonsaveinstancestate (Bundle outstate) {Super. Onsaveinstancestate (outstate); Outstate.putstring ("url", URL); Outstate.putint ("Index", index); } @Override PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancestate) { if(Savedinstancestate! =NULL) {URL= savedinstancestate.getstring ("url"); Index= Savedinstancestate.getint ("index"); } View View= Inflater.inflate (R.layout.view_photo, container,false); Image=(ImageView) View.findviewbyid (r.id.image); returnview; } @Override Public voidonactivitycreated (@Nullable Bundle savedinstancestate) {Super. onactivitycreated (savedinstancestate); } @Override Public voidSetuservisiblehint (BooleanIsvisibletouser) {        Super. Setuservisiblehint (Isvisibletouser); if(Isvisibletouser) {log.i (TAG,"Get pic data for show");        Imageloader.getinstance (). DisplayImage (URL, image); }        }}

The above is the core part of the code;

SOURCE Demo:http://files.cnblogs.com/kingfly13/viewpagernocache.zip

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.