Simple use of Android development Viewpager

Source: Internet
Author: User

Viewpager is in the V4 package, if your compiler cannot knock out Viewpager, then you need to add, see below:

First step: Click on the + sign

Step Two: Select the first library

Step Three: Add this package:

Then click on Ok-->ok to get it done, and the compiler will precompile from the new.

Here's the point, we'll look at the layout file:

1 < Android.support.v4.view.ViewPager 2         Android:id = "@+id/viewpager" 3         android:layout_width= "Match_parent"4        android:layout_height = "Match_parent" 5         />

PS: There is an automatic prompt when you knock, which means you added success.

Then the code:

1  Public classMainactivityextendsActivity {2 3     PrivateViewpager Viewpager;4     PrivateArraylist<imageview>mimageviewlist;5     Private int[] Mimagerids =New int[]{r.drawable.guide_1,r.drawable.guide_2,r.drawable.guide_3};6 7 @Override8     protected voidonCreate (Bundle savedinstancestate) {9         Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.activity_main); One  AViewpager =(Viewpager) Findviewbyid (R.id.viewpager); - InitData (); -Viewpageradapter adapter =NewViewpageradapter (); the Viewpager.setadapter (adapter); -     } -  -  +  -      Public voidInitData () { +Mimageviewlist =NewArraylist<>(); A         //Add all the ImageView to the Mimageviewlist collection at          for(inti=0; i<mimagerids.length; i++){ -             //Create a new imgeview add a picture resource to the ImageView -ImageView ImageView =NewImageView ( This); - Imageview.setbackgroundresource (Mimagerids[i]); -             //add ImageView to the collection of Mimageviewlist - Mimageviewlist.add (imageView); in         } -  to     } +  -     classViewpageradapterextendsPageradapter { the  *         //Number of item $ @OverridePanax Notoginseng          Public intGetCount () { -             returnmimageviewlist.size (); the         } +  A @Override the          Public Booleanisviewfromobject (View view, Object object) { +             returnview==object; -         } $  $         //Initialize Item layout - @Override -          PublicObject Instantiateitem (ViewGroup container,intposition) { theImageView ImageView =Mimageviewlist.get (position); -Container.addview (ImageView);//Add the ImageView inWuyi             returnImageView;//return the ImageView the         } -  Wu         //Destroy Item - @Override About          Public voidDestroyitem (ViewGroup container,intposition, Object object) { $             //Super.destroyitem (container, Position, object);//If you use this one item to slip to the last one will give an error - Container.removeview ((View) object); -         } -     } A}

Idea: First put the picture resources into a int[], and then in the for loop inside the loop create ImageView set the picture resource, then add ImageView to the ArrayList collection, and then initialize the item in the Instantiateitem () method, Add the ImageView in the ImageView collection to ViewGroup, then return to ImageView, and destroy ImageView without Super.destroyitem (container, Position, object); This will cause the slip to the last imageview times wrong.

Simple use of Android development 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.