Android:viewpager Making Slides

Source: Internet
Author: User

Layout:

<?XML version= "1.0" encoding= "Utf-8"?>   <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "100DP"    >    <Android.support.v4.view.ViewPagerAndroid:id= "@+id/viewpager"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"    ></Android.support.v4.view.ViewPager>    <LinearLayoutAndroid:id= "@+id/pointgroup"Android:layout_width= "Match_parent"Android:layout_height= "10DP"android:orientation= "Horizontal"Android:layout_alignparentbottom= "true"Android:layout_marginbottom= "5DP"android:gravity= "Center"    >    </LinearLayout></Relativelayout>

Program:

 Public classMainactivityextendsActivity {//Viewpager        PrivateViewpager Viewpager; //Slideshow Picture Resources        Private int[] imgarray={R.DRAWABLE.VIEW1,R.DRAWABLE.VIEW2,R.DRAWABLE.VIEW3}; //Image Array        PrivateArraylist<view>viewlist; PrivateImageView ImageView; //Dot Dot        PrivateViewGroup Pointgroup; Privateimageview[] Pointarr; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub            Super. OnCreate (savedinstancestate);            Setcontentview (R.layout.activity_viewpage); //InitializeViewpager =(Viewpager) Findviewbyid (R.id.viewpager); Pointgroup=(ViewGroup) Findviewbyid (R.id.pointgroup); Viewlist=NewArraylist<view>(); //to mount a picture to an array             for(inti=0;i) {ImageView=NewImageView ( This); //set up a pictureImageview.setbackgroundresource (Imgarray[i]); //Set Picture IDImageview.setid (Imgarray[i]);                            Viewlist.add (ImageView); }                        /** Viewpager Load Adapter * MyPageAdapter to Custom adapter **/                        //Delivery ContextContext context = Mainactivity. This; MyPageAdapter Pageadapter2=NewMyPageAdapter (Viewlist,context);                                            Viewpager.setadapter (PAGEADAPTER2); //Points GroupPointarr=NewImageview[imgarray.length]; //add points to the layout, and set the point State             for(inti=0;i) {ImageView point=NewImageView ( This); //Set point sizePoint.setlayoutparams (NewLayoutparams (10,10)); Pointarr[i]=Point ; //State Processing                if(i==0) {Pointarr[i].setbackgroundresource (r.drawable.viewpage_point_focused); }Else{pointarr[i].setbackgroundresource (r.drawable.viewpage_point_unfocused); }                //Add to ContainerPointgroup.addview (Pointarr[i]); }                                //Viewpager setting up listenersPointchangelistener pointlistener=NewPointchangelistener ();            Viewpager.setonpagechangelistener (Pointlistener); //Click image                                            }                            /** ViewPage Listener*/                 Public classPointchangelistenerImplementsonpagechangelistener{@Override Public voidOnpagescrollstatechanged (intarg0) {                //TODO auto-generated Method Stub} @Override Public voidOnpagescrolled (intARG0,floatArg1,intarg2) {                //TODO auto-generated Method Stub} @Override//For example, to switch to the second picture, the point of the corresponding index is select State, the other is unchecked             Public voidOnpageselected (intarg0) {                                //TODO auto-generated Method Stub                 for(inti=0;i<pointarr.length;i++) {Pointarr[arg0].setbackgroundresource (r.drawable.viewpage_point_focused); if(arg0! =i)                        {Pointarr[i].setbackgroundresource (r.drawable.viewpage_point_unfocused); }                }                            }                                }                    }

Custom adapters:

 Public classMyPageAdapterextendsPageradapter {PrivateList<view>viewlist; Privatecontext Context;  PublicMyPageAdapter (list<view>Viewlist,context Context) {         This. viewlist =viewlist;  This. context=context; } @Override Public intGetCount () {//TODO auto-generated Method Stub        returnviewlist.size (); } @Override Public BooleanIsviewfromobject (View arg0, Object arg1) {//TODO auto-generated Method Stub        returnarg0==arg1; }        //The following are also necessary methods, but to add your own//instantiation of@Override PublicObject Instantiateitem (View container,intposition) {        //TODO auto-generated Method Stub((Viewpager) container). AddView (Viewlist.get (position)); View View=Viewlist.get (position); View.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub                inti =V.getid (); String T= "You clicked on the picture ID for the picture:" +i;                                Toast.maketext (context, T, Toast.length_long). Show (); /*jump page *intent Intent =new Intent (context,navigationactivity.class);                  *context.startactivity (Intent); */                         }                    }); returnViewlist.get (position); }         //destroyed@Override Public voidDestroyitem (ViewGroup container,intposition, Object object) {        //TODO auto-generated Method StubContainer.removeview (viewlist.get (position)); }        }

Example Download >>>>>>>>

Android:viewpager Making Slides

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.