Android uses Viewpager to achieve the right and left loop slide automatic playback _android

Source: Internet
Author: User
Tags array length object object

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 first 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 VIEWPA 
Ger 
/** * 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 resource id Imgidarray = 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 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 GetCount () {R 
Eturn 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 (MI 
Mageviews[position% mimageviews.length]); /** * Load the picture in, with the current position divided by the image array length to get the remainder is the key * * @Override public Object instantiateitem (View container, int position) 
{((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 arg1, int arg2) {} @Override public void onpageselected (int arg0) {Setimagebackgroun 
D (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);  } 
} 
} 
}

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) add exception Capture
[Java] View Plain copy 
@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 is a small set for you to introduce the Android using Viewpager to achieve the picture around the circular sliding automatic playback of all the narration, hope that we have some help, if you have any questions, welcome to my message, small series will promptly reply to everyone, and this is also very grateful to you for your support cloud Habitat community site!

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.