Android skillfully use Viewpager to achieve left and right circular sliding picture _android

Source: Internet
Author: User
Tags array length object object

Viewpager This small demo is to be able to loop around the picture, the following index, slide to the last page in the right slide will be the first page, the first page to the left slide to the last page, first on the effect of the picture:

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" > <androi D.support.v4.view.viewpager android:id= "@+id/viewpager" android:layout_width= "Fill_parent" android:layou t_height= "Wrap_content"/> <relativelayout android:layout_width= "Fill_parent" Android:layout_h eight= "wrap_content" android:orientation= "vertical" > <linearlayout android:id= "@+id/viewgrou" P "android:layout_width=" Fill_parent "android:layout_height=" Wrap_content "Android:layout_alignpar Entbottom= "true" android:layout_marginbottom= "30DP" android:gravity= "Center_horizontal" Android:or ientation= "Horizontal" > </LinearLayout> </RelativeLayout> </FrameLayout> 

  Use Viewpager first 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 VIEWPA 
   
  GER Viewpager; 
   
  /** * 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 (LayoutP Arams. 
      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 () {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) containe R). 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 C 
      Ontainer, int position) {((Viewpager) container). AddView (mimageviews[position% mimageviews.length], 0); 
    Return mimageviews[position% mimageviews.length]; @Override public void onpagescrollstatechanged (int arg0) {} @Override P ublic void onpagescrolled (int arg0, float arg1, int arg2) {} @Override public void onpageselected (int ar 
  G0) {setimagebackground (arg0% mimageviews.length); /** * Set the background of selected Tip * @param selectitems * * * private void Setimagebackground (int selectitems) {f or (int i=0; i<tips.length; i++) {if (i = = Selectitems) {Tips[i].setbackgroundresource (r.drawable.page_in 
      dicator_focused); }else{Tips[i].setbackgroundresource (r.drawable.page_indicator_unfocused); 
      } 
    } 
  } 
 
}  

SOURCE download: Left and right sliding Viewpager

2, in the above code, when there are only 3 pictures or 2 pictures, the sliding 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 abnormal catch
@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]; 
  } 

This is the entire content of this article, I hope to learn more about Android software programming help.

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.