Switch between the left and right of the screen implemented by viewPager in android (Getting Started)

Source: Internet
Author: User

Switch between the left and right of the screen implemented by viewPager in android (Getting Started)
Most apps can switch back and forth several screens. First, two new activities are created with random content and layout. Next, in MainActivity. xml: Copy code 1 <RelativeLayout xmlns: 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. viewPager 8 android: id = "@ + id/viewpager" 9 android: layout_width = "wrap_content" 10 android: layout_height = "wrap_content"> 11 </android. support. v4.view. viewPager> 12 13 </RelativeLayou copy Code do not forget the android-support-v4 package, the current version of 4.0 and later comes with the method in java: copy the Code 1 protected void onCreate (Bundle savedInstanceState) {2 super. onCreate (savedInstanceState); 3 setContentView (R. layout. activity_main); 4 viewPager = (ViewPager) findViewById (R. id. viewpager); 5 View view1 = LayoutInflater. from (MainActivity. this ). inflate (R. layout. a, 6 null); 7 View view2 = LayoutInflater. from (MainActivity. this ). inflate (R. layout. b, 8 null); 9 list = new ArrayList <View> (); 10 list. add (view1); 11 list. add (view2); 12 viewPager. setAdapter (new MyAdapter (); 13} copy code 1 class MyAdapter extends PagerAdapter {2 3 @ Override 4 public int getCount () {5 return list. size (); 6} 7 8 @ Override 9 public boolean isViewFromObject (View arg0, Object arg1) {10 return arg0 = arg1; 11} 12 13 @ Override14 public CharSequence getPageTitle (int position) {15 return titlelist. get (position); 16 17} 18 19 @ Override20 public Object instantiateItem (View container, int position) {21 (ViewPager) container ). addView (list. get (position); 22 return list. get (position); 23} 24 25 @ Override26 public void destroyItem (View container, int position, Object object) {27 super. destroyItem (container, position, object); 28 (ViewPager) container ). removeView (list. get (position); 29} 30}

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