Banner Infinite Carousel

Source: Internet
Author: User

Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <relativelayout android:layout_width= "Match_parent"Android:layout_height= "200DP" > <Android.support.v4.view.ViewPager Android:id= "@+id/viewpager"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"/> <LinearLayout Android:id= "@+id/dotlayout"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentbottom= "true"Android:layout_centerhorizontal= "true"Android:layout_marginbottom= "20DP"android:gravity= "Center"android:orientation= "Horizontal"/> </RelativeLayout></RelativeLayout>

Selecter.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_enabled=" true ">        <shape android:shape=" Oval ">            <size android:height= "10DP" android:width= "10DP" ></size>            <solid android:color= "#aa000000" ></ solid>        </shape>    </item>    <item android:state_enabled= "false" >        <shape Android:shape= "Oval" >            <size android:height= "10DP" android:width= "10DP" ></size>            <solid Android:color= "#aaffffff" ></solid>        </shape>    </item></selector>

 Public classMainactivityextendsappcompatactivity {PrivateList<imageview>list; PrivateLinearLayout dotlayout; Private intpreposition; PrivateViewpager Viewpager; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        InitData ();        Initview (); //open a new thread for automatic scrolling of Viewpager        NewThread (NewRunnable () {@Override Public voidrun () { while(true) {                    Try{Thread.Sleep (3000); } Catch(interruptedexception e) {e.printstacktrace (); }                    //the code in the Runonuithread runs in the main threadRunonuithread (NewRunnable () {@Override Public voidrun () {Viewpager.setcurrentitem (Viewpager.getcurrentitem ()-W);                }                    });    }}). Start (); }    Private voidInitview () {Viewpager=(Viewpager) Findviewbyid (R.id.viewpager); //Constructing AdaptersMyadapter adapter =Newmyadapter (list); //Set AdapterViewpager.setadapter (adapter); //set the number of Viewpager preloaded pages//Viewpager.setoffscreenpagelimit (2);Viewpager.addonpagechangelistener (NewViewpager.onpagechangelistener () {//Viewpager is called during the sliding process@Override Public voidOnpagescrolled (intPositionfloatPositionoffset,intpositionoffsetpixels) {            }            //Viewpager A new page is determined and then called@Override Public voidOnpageselected (intposition) {                //set the previous view to whiteDotlayout.getchildat (preposition). setenabled (false); //set Current view to RedDotlayout.getchildat (position% list.size ()). SetEnabled (true); //assigns the current location to prepositionpreposition = position%list.size (); }            //Viewpager The sliding state changes after the call//0--Static State//the hand is dragging//2--Auto Slide after hand release@Override Public voidOnpagescrollstatechanged (intState )        {            }        }); //set the initial item for ViewpagerViewpager.setcurrentitem (INTEGER.MAX_VALUE/2-3); }    /*** Construct data source*/    Private voidInitData () {dotlayout=(LinearLayout) Findviewbyid (r.id.dotlayout); List=NewArraylist<>(); int[] IMGs =New int[]{r.drawable.a, r.drawable.b, r.drawable.c, R.DRAWABLE.D, R.DRAWABLE.E};  for(intImg:imgs) {            //Create a ImageView using Java codeImageView ImageView =NewImageView ( This); //set picture and ScaleType propertiesImageview.setimageresource (IMG);            Imageview.setscaletype (ImageView.ScaleType.CENTER_CROP);            List.add (ImageView); //Create a View objectView view =NewView ( This); //set the width and height of the viewLinearlayout.layoutparams params =NewLinearlayout.layoutparams ((int) Typedvalue.applydimension (Typedvalue.complex_unit_dip, 8, Getresources (). Getdisplaymetrics ()), (int) Typedvalue.applydimension (Typedvalue.complex_unit_dip, 8, Getresources (). Getdisplaymetrics ()); //set view left margin to prevent view from being linked togetherParams.leftmargin = (int) Typedvalue.applydimension (Typedvalue.complex_unit_dip, 12, Getresources (). Getdisplaymetrics ());            View.setlayoutparams (params); //set the background color, the background uses the selector, can make the view show different colors in different statesView.setbackgroundresource (R.drawable.dot_radiobutton_style); //The enable property of view is false by default (view is all white)View.setenabled (false); //add view into LinearLayoutDotlayout.addview (view); }        //the first view defaults to redDotlayout.getchildat (0). setenabled (true); }}

Banner Infinite Carousel

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.