Viewpager This small demo implementation is can be left and right loop slide picture, below with index, slide to the last page in the first page, the first page to the left to the last page, first, with beautiful pictures is my usual style, hehe
1. First look at some of the layout of the XML
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <android.support.v4.view.viewpager Android:id= "@+id/viewpager" android:layout_width= "fill_parent" android:layout_height= "wrap_content "/> <relativelayout android:layout_width=" fill_parent "android:layout_height=" Wrap_co Ntent "android:orientation=" vertical "> <linearlayout android:id=" @+id/viewgroup " Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layou T_alignparentbottom= "true" android:layout_marginbottom= "30DP" android:gravity= "Center_horizontal" android:orientation= "Horizontal" > </LinearLayout> </RelativeLayout> </fram Elayout>
Using Viewpager first needs to introduce Android-support-v4.jar this jar package. Don't forget to add
Package Com.example.viewpagerdemo;import Android.app.activity;import Android.os.bundle;import Android.support.v4.view.pageradapter;import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.view.view;import Android.view.ViewGroup; Import Android.view.viewgroup.layoutparams;import Android.widget.imageview;import android.widget.LinearLayout; public class Twoactivity extends Activity implements onpagechangelistener{/** * Viewpager */private viewpager viewpager;/ * * Decorate point ImageView array */private imageview[] tips;/** * Install ImageView array */private imageview[] mimageviews;/** * Picture resource ID */privat e int[] imgidarray; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); ViewGroup Group = (viewgroup) Findviewbyid (r.id.viewgroup); Viewpager = (Viewpager) Findviewbyid (R.id.viewpager);// Load Picture Resource Idimgidarray = new int[]{r.drawable.item01, r.drawable.item02, R.drawable.item03, R.Drawable.item04,r.drawable.item05,r.drawable.item06, R.drawable.item07, r.drawable.item08};// Add point points to viewgroup tips = new imageview[imgidarray.length];for (int i=0; i<tips.length; i++) {ImageView ImageView = new ImageView (this); Imageview.setlayoutparams (New Layoutparams (10,10)); Tips[i] = ImageView; if (i = = 0) {tips[i].setbackgroundresource (r.drawable.page_indicator_focused); }else{Tips[i].setbackgroundresource (r.drawable.page_indicator_unfocused); } linearlayout.layoutparams layoutparams = new Linearlayout.layoutparams (New Viewgroup.layoutparams (LayoutParams.WR Ap_content, layoutparams.wrap_content)); Layoutparams.leftmargin = 5; Layoutparams.rightmargin = 5; Group.addview (ImageView, layoutparams);} Load a picture into an array mimageviews = new imageview[imgidarray.length];for (int i=0; i<mimageviews.length; i++) {ImageView ImageView = new ImageView (this); Mimageviews[i] = Imageview;imageview.setbackgroundresource (Imgidarray[i]);} Set AdapterviewPager.setadapter (New Myadapter ());//Set monitoring, mainly set the point of the background viewpager.setonpagechangelistener (this);//Set the Viewpager default, Set to 100 times times the length, this way you can swipe left Viewpager.setcurrentitem ((mimageviews.length) * 100);} /** * * @author xiaanming * */public class Myadapter extends pageradapter{@Overridepublic int GetCount () {return Integer. Max_value;} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;} @Overridepublic void Destroyitem (View container, int position, object object) {(Viewpager) container). Removeview ( Mimageviews[position% mimageviews.length]);} /** * Loading the image in, dividing the current position by the length of the image array is the key/@Overridepublic Object Instantiateitem (View container, int position) {(VIEWP Ager) container). AddView (mimageviews[position% mimageviews.length], 0); return mimageviews[position% Mimageviews.length];}} @Overridepublic void onpagescrollstatechanged (int arg0) {} @Overridepublic void onpagescrolled (int arg0, float arg1, int a RG2) {} @Overridepublic void onpageselected (int arg0) {SetimagebackgrounD (arg0% mimageviews.length);} /** * Sets the background of the selected Tip * @param selectitems */private void setimagebackground (int selectitems) {for (int i=0; i<tips.length; i + +) {if (i = = Selectitems) {tips[i].setbackgroundresource (r.drawable.page_indicator_focused);} Else{tips[i].setbackgroundresource (r.drawable.page_indicator_unfocused);}}}
Code download
In the above code, when there are only 3 pictures or 2 pictures, the bug of sliding has the following changes
- Destroyitem (View container, int position, Object object) method does not Removeview
@Overridepublic void Destroyitem (View container, int position, object object) {//((Viewpager) container). Removeview ( Mimageviews[position% mimageviews.length]);}
- Instantiateitem (View container, int position) Increase exception capture
@Overridepublic Object Instantiateitem (View container, int position) {try { (Viewpager) container). AddView ( Mimageviews[position% mimageviews.length], 0);} catch (Exception e) {//handler something}return mimageviews[position% mimageviews.length];}
- The implementation of the previous Android ScrollView bounce effect
- Next article directly to use! The hottest Android Open source project (end of article)
Android uses Viewpager to make the left and right loops slide pictures