Android using Viewpager to implement pictures can be left and right loop sliding effect attached code download _android

Source: Internet
Author: User
Tags array length object object

First of all to show you beautiful photos, the effect of interested friends can continue reading oh.

Viewpager This small demo is to be able to scroll around the picture, the following with index, slide to the last page in the right slide to the first page, the first page to the left slide to the last page, the above is the effect of the picture, with beautiful pictures is my consistent style, hehe

1. First look at some layout under 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_content" 
 android:orientation= "vertical" > 
 <linearlayout 
 Android:id= "@+id/viewgroup" 
 android:layout_width= "fill_parent" 
 android:layout_height= "Wrap_content" 
 android:layout_alignparentbottom= "true" 
 android:layout_marginbottom= "30DP" 
 android:gravity= " Center_horizontal " 
 android:orientation=" Horizontal "> 
 </LinearLayout> 
 </ Relativelayout> 

Using Viewpager first you need 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 VI 
 Ewpager; 
 /** * Decorate point of the imageview array * * private imageview[] tips; 
 /** * Loaded ImageView array/private imageview[] mimageviews; 
 /** * Picture Resource ID * * Private int[] Imgidarray; 
 @Override protected 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 ResourcesID Imgidarray = new INT[]{R.DRAWABLE.ITEM01, r.drawable.item02, R.drawable.item03, r.drawable.item04, R.drawable.item0 
 5,r.drawable.item06, r.drawable.item07, r.drawable.item08}; 
 Add dots to the 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.wrap_content, layoutparams.wrap_content)); 
 Layoutparams.leftmargin = 5; 
 Layoutparams.rightmargin = 5; 
 Group.addview (ImageView, layoutparams); 
 //load the picture into the 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 adapter Viewpager.setadapter (new Myadapter ()); 
 Set up the listening, mainly set the point of the background viewpager.setonpagechangelistener (this); 
 Set the default entry for the Viewpager, set to 100 times times the length, so that you can start sliding to the left Viewpager.setcurrentitem (mimageviews.length) * 100); /** * * * @author xiaanming * */public class Myadapter extends pageradapter{@Override public int Getcoun 
 T () {return integer.max_value; 
 @Override public boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = Arg1; @Override public void Destroyitem (View container, int position, object object) {(Viewpager) container). Removeview 
 (mimageviews[position% mimageviews.length]); /** * Load the picture in, with the current position divided by the image array length to the remainder is the key/@Override public Object instantiateitem (View container, int posit 
 Ion) {(Viewpager) container). AddView (mimageviews[position% mimageviews.length], 0); 
 Return mimageviews[position% mimageviews.length]; } 
 } 
 @Override public void onpagescrollstatechanged (int arg0) {} @Override public void onpagescrolled (int arg0, float a  
 RG1, int arg2) {} @Override public void onpageselected (int arg0) {setimagebackground (arg0% mimageviews.length); /** * Set the background of 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);  } 
 } 
 } 
}

Like friends directly download the code OH. Code download

In the code above, when there are only 3 pictures or 2 pictures, the slide bug problem is modified as follows

Destroyitem (View container, int position, object) method does not Removeview

 @Override public 
 void Destroyitem (View container, int position, object object) { 
//(Viewpager) container). Removeview (mimageviews[position% mimageviews.length]); 
 } 
Instantiateitem (view container, int position) Increase the exception capture
[Java] View plaincopy the code to see how it is derived from my Code slice
@Override 
 Public 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 above code to show you android using Viewpager to achieve the image can be left and right circular sliding effect, I hope you like.

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.