One of Android's ever-changing Viewpager

Source: Internet
Author: User

Believe that everyone is not unfamiliar with Viewpager, Viewpager is a view switch role. Today I will tell you how to use viewpager differently.

First, the original Viewpager

Before, I was so using Viewpager, on an activity to set Viewpager, can be through the swipe of the fingers to switch between different view, I added a different function on each view, so that can be achieved, multi-function in one activity.

Second, the present Viewpager

We usually use a lot of app, there will be a small detail, that is, a lot of apps in, there is a buffer interface, the picture on the interface is the introduction of this application, I believe many people have this discovery. However, when we develop the app, we can not help but wonder, how to achieve that function? This will lead us to the protagonist--viewpager today. In fact, it is a welcome interface made with Viewpager. So today we'll see how to use it to switch pictures

Third, the use of Viewpager

First of all to review the previous usage, I used to use this, first by the activity of the layout file, followed by a lot of layout files, these are the viewpager of the sub-view layout, Use the Inflate method in the Java code to convert the XML file to view, add the converted view to the ArrayList, and load ArrayList by implementing Pageradapter to generate a new adapter. Finally, the Viewpager is used to load the adapter, thus achieving the requirements.

Now we are different, using adapter directly to load ImageView.

Code Show

1  PackageCom.example.android_viewpager;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 Importandroid.app.Activity;7 ImportAndroid.os.Bundle;8 ImportAndroid.support.v4.view.PagerAdapter;9 ImportAndroid.support.v4.view.ViewPager;Ten ImportAndroid.view.View; One ImportAndroid.view.ViewGroup; A ImportAndroid.widget.ImageView; - ImportAndroid.widget.ImageView.ScaleType; -  the  Public classMainactivityextendsActivity { -     PrivateViewpager Viewpager =NULL; -     Private int[] res =New int[] {r.drawable.guide_image1, r.drawable.guide_image2, r.drawable.guide_image3}; -     PrivateList<imageview>list =NewArraylist<imageview>(); +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); AViewpager =(Viewpager) Findviewbyid (R.id.viewpager); -Viewpager.setadapter (NewPageradapter () { - @Override -              PublicObject Instantiateitem (ViewGroup container,intposition) { -ImageView ImageView =NewImageView (mainactivity. This); - Imageview.setimageresource (res[position]); in Imageview.setscaletype (scaletype.center_crop); - Container.addview (ImageView); to List.add (ImageView); +                 returnImageView; -             } the @Override *              Public voidDestroyitem (ViewGroup container,intposition, Object object) { $                 //TODO auto-generated Method StubPanax Notoginseng Container.removeview (list.get (position)); -             } the              Public BooleanIsviewfromobject (View arg0, Object arg1) { +                 //TODO auto-generated Method Stub A                 returnarg0 = =arg1; the             } +              - @Override $              Public intGetCount () { $                 //TODO auto-generated Method Stub -                 returnres.length; -             } the         }); -     }Wuyi}

The above code to achieve the Viewpager switch multiple images, if there is a lag in the middle or throw a OutOfMemoryError exception, please move the picture to the highest resolution of the drawable, so there is no problem.

One of Android's ever-changing Viewpager

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.