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

Source: Internet
Author: User

Viewpager, which is a class of an add-on package that comes with the Google SDK, lets you slide the view.

Steps:

1, introduce Android-support-v4.jar package, add in the main layout

< Android.support.v4.view.ViewPager Android:id = "@+id/viewpager"   android:layout_width= "Match_parent"android:layout_height= "Wrap_content"  >       </android.support.v4.view.ViewPager>

2. Load Switch Content

3. Viewpager Load Adapter Pageradapter

 Public classMainactivityextendsActivity {PrivateViewpager Viewpager;PrivateArraylist<view>PageView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Set no title barrequestwindowfeature (Window.feature_no_title);                Setcontentview (R.layout.main); Viewpager=(Viewpager) Findviewbyid (R.id.viewpager); //find layout files with Layoutinflater.inflateLayoutinflater Inflater =Getlayoutinflater (); View View1= Inflater.inflate (R.LAYOUT.ITEM01,NULL); View View2= Inflater.inflate (R.LAYOUT.ITEM02,NULL); View VIEW3= Inflater.inflate (R.layout.item03,NULL); //to load a view into an arrayPageView =NewArraylist<view>();        Pageview.add (View1);        Pageview.add (VIEW2);                        Pageview.add (VIEW3); //Data AdapterPageradapter Mpageradapter =NewPageradapter () {@Override//gets the current form interface number             Public intGetCount () {//TODO auto-generated Method Stub                returnpageview.size (); } @Override//whether the interface is generated by the object             Public BooleanIsviewfromobject (View arg0, Object arg1) {//TODO auto-generated Method Stub                returnarg0==arg1; }            //is to remove the current view from the ViewGroup              Public voidDestroyitem (View arg0,intarg1, Object arg2)                  {(Viewpager) arg0). Removeview (Pageview.get (arg1)); }                          //returns an object that indicates which object the Pageradapter adapter chooses to place in the current Viewpager             PublicObject Instantiateitem (View arg0,intarg1)                {(Viewpager) arg0). AddView (Pageview.get (arg1)); returnPageview.get (ARG1);                }                                }; //Binding AdapterViewpager.setadapter (Mpageradapter); }}

>>>>>>>>>>>>> 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.