[Android] Carousel diagram-Infinite loop

Source: Internet
Author: User

Implementing an infinite loop

In the GetCount () method, returns a very large value,integer.max_value

In the Instantiateitem () method, when you get the index of the current View , take the action and pass in the int position It was a very large number, and he was asked for the remainder.

In the Destroyitem () method, the same

In the onpageselected () Listener method, the passed in index is taken from the

Inverse Infinite loop

Call the Setcurrentitem () method of the Viewpager object , set the current page when the first time it comes in, parameters:int number, We position it in the middle of the integer.max_value and ensure that the first one is the current position,integer.max_value/2-(INTEGER.MAX_VALUE/2) %banners.size ())

There's a problem with a left-drawn blank page right now.

 PackageCom.tsh.myviewpager;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.support.v4.view.ViewPager.OnPageChangeListener;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateViewpager Vp_banner; PrivateLinearLayout ll_points; PrivateTextView Tv_title; PrivateList<view>banners; PrivateString[] Titles=Newstring[]{"News Title 1",            "News Title 2",            "News Title 3"    }; PrivateList<imageview> points=NewArraylist<imageview>(); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //InitializeVp_banner =(Viewpager) Findviewbyid (R.id.vp_banner); Ll_points=(LinearLayout) Findviewbyid (r.id.ll_points); Tv_title=(TextView) Findviewbyid (r.id.tv_title); //Banner PartBanners =NewArraylist<>(); ImageView IMG1=NewImageView ( This);        Img1.setimageresource (R.DRAWABLE.A);        Banners.add (IMG1); ImageView Img2=NewImageView ( This);        Img2.setimageresource (r.drawable.b);        Banners.add (IMG2); ImageView IMG3=NewImageView ( This);        Img3.setimageresource (R.DRAWABLE.C);                Banners.add (IMG3); //Small Iconsll_points=(LinearLayout) Findviewbyid (r.id.ll_points);  for(intI=0;i<banners.size (); i++) {ImageView image=NewImageView ( This);            Image.setimageresource (R.drawable.point_selector); Linearlayout.layoutparams params=NewLinearlayout.layoutparams (5, 5); Params.rightmargin=4;                        Image.setlayoutparams (params); if(i==0) {image.setenabled (true); }Else{image.setenabled (false);            } points.add (image);        Ll_points.addview (image); }                //setting up the adapterVp_banner.setadapter (NewMypageradapter ()); Vp_banner.setcurrentitem (Integer.max_value/2-((INTEGER.MAX_VALUE/2)%banners.size ())); Vp_banner.setonpagechangelistener (NewOnpagechangelistener () {@Override Public voidOnpageselected (intarg0) {Tv_title.settext (titles[arg0%banners.size ()]); //Small icon Handling                 for(ImageView point:points) {point.setenabled (false); } points.get (arg0%banners.size ()). SetEnabled (true); } @Override Public voidOnpagescrolled (intARG0,floatArg1,intarg2) {                //TODO auto-generated Method Stub} @Override Public voidOnpagescrollstatechanged (intarg0) {                //TODO auto-generated Method Stub                            }        }); }    Private classMypageradapterextendsPageradapter {@Override Public voidDestroyitem (ViewGroup container,intposition, Object object) {System.out.println ("Destroy:" +position);            Container.removeview ((View) object); Object=NULL; } @Override PublicObject Instantiateitem (ViewGroup container,intposition) {System.out.println ("Initialize:" +position); Position=position%banners.size (); //Fix Right Stroke error            if(Banners.get (position). getParent ()! =NULL){                //Container.removeview (banners.get (position));}Else{Container.addview (banners.get (position)); }                        returnBanners.get (position); } @Override Public intGetCount () {returnInteger.max_value; } @Override Public BooleanIsviewfromobject (View arg0, Object arg1) {returnarg0 = =arg1; }    }}

[Android] Carousel diagram-Infinite loop

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.