Viewpager implementation of Android in the screen left and right switch (introductory article)

Source: Internet
Author: User

Most apps can switch back and forth on several screens,

First create two new activity, content arbitrary, layout arbitrary. Next in Mainactivity.xml:

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Tools:context=". Mainactivity " >6 7     <Android.support.v4.view.ViewPager8         Android:id= "@+id/viewpager"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" > One     </Android.support.v4.view.ViewPager> A  - </relativelayou
Do not forget the ANDROID-SUPPORT-V4 bag, currently 4.0 or more of the version comes with

Next is the method in Java:
1 protected voidonCreate (Bundle savedinstancestate) {2         Super. OnCreate (savedinstancestate);3 Setcontentview (r.layout.activity_main);4Viewpager =(Viewpager) Findviewbyid (R.id.viewpager);5View view1 = Layoutinflater.from (mainactivity. This). Inflate (R.LAYOUT.A,6                 NULL);7View view2 = Layoutinflater.from (mainactivity. This). Inflate (r.layout.b,8                 NULL);9List =NewArraylist<view>();Ten List.add (view1); One List.add (VIEW2); AViewpager.setadapter (NewMyadapter ()); -}
1 classMyadapterextendsPageradapter {2 3 @Override4          Public intGetCount () {5             returnlist.size ();6         }7 8 @Override9          Public BooleanIsviewfromobject (View arg0, Object arg1) {Ten             returnarg0 = =arg1; One         } A  - @Override -          PublicCharsequence Getpagetitle (intposition) { the             returnTitlelist.get (position); -  -         } -  + @Override -          PublicObject Instantiateitem (View container,intposition) { + ((Viewpager) container). AddView (List.get (position)); A             returnList.get (position); at         } -  - @Override -          Public voidDestroyitem (View container,intposition, Object object) { -             Super. Destroyitem (container, Position, object); - ((Viewpager) container). Removeview (List.get (position)); in         } -}

Now you can switch between two screens.

Viewpager implementation of Android in the screen left and right switch (introductory article)

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.