Android: Use Viewpager to move left and right to switch pictures (a little bit on the picture)

Source: Internet
Author: User

Add points based on the following example

Android: Use Viewpager to move left and right to toggle pictures (simple version)

Effect preview:

Because you want to place the dots on the image, modify the layout to a relative 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= "Match_parent"android:orientation= "vertical" >    <!--Switch Card -    <Android.support.v4.view.ViewPagerAndroid:id= "@+id/viewpager"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"        >      </Android.support.v4.view.ViewPager>          <!--Dot Dot -           <LinearLayoutAndroid:id= "@+id/viewgroup"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentbottom= "true"Android:layout_marginbottom= "40DP"android:gravity= "Center_horizontal"android:orientation= "Horizontal" >             </LinearLayout>  </Relativelayout>

Program added:

Privateprivate// parcel Point linearlayoutprivate ViewGroup Group
Group =(ViewGroup) Findviewbyid (R.id.viewgroup); //How many pictures there are , how many dots?Imageviews =Newimageview[pageview.size ()];  for(intI =0;i<pageview.size (); i++) {ImageView=NewImageView (mainactivity. This); Imageview.setlayoutparams (NewLayoutparams (20,20)); Imageview.setpadding (20, 0, 20, 0); Imageviews[i]=ImageView; //The default first graph shows selected state            if(i = = 0) {Imageviews[i].setbackgroundresource (r.drawable.page_indicator_focused); } Else{imageviews[i].setbackgroundresource (r.drawable.page_indicator_unfocused);          } group.addview (Imageviews[i]); }                                        //PageView Listener        classGuidepagechangelistenerImplementsonpagechangelistener{@Override Public voidOnpagescrollstatechanged (intarg0) {                //TODO auto-generated Method Stub} @Override Public voidOnpagescrolled (intARG0,floatArg1,intarg2) {                //TODO auto-generated Method Stub} @Override//If you switch, set the current point to the selected background, and the other settings do not have the background selected .             Public voidOnpageselected (intarg0) {                //TODO auto-generated Method Stub                 for(inti=0;i<imageviews.length;i++) {Imageviews[arg0].setbackgroundresource (r.drawable.page_indicator_focused); if(arg0! =i) {imageviews[i].setbackgroundresource (r.drawable.page_indicator_unfocused); }                  }                            }                    }                //Binding Listener EventsViewpager.setonpagechangelistener (NewGuidepagechangelistener ());

>>>>>>>>>>>>>>>> Example Download

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.