Android 使用ViewPager实现左右循环滑动图片

ViewPager这个小demo实现的是可以左右循环滑动图片,下面带索引,滑到最后一页在往右滑动就要第一页,第一页往左滑动就到最后一页,先上效果图,用美女图片是我一贯的作风,呵呵

1.    首先看一些layout下的xml

 

[html] view plaincopy
 
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:orientation="vertical" >  
  5.       
  6.    <android.support.v4.view.ViewPager    
  7.         android:id="@+id/viewPager"    
  8.         android:layout_width="fill_parent"    
  9.         android:layout_height="wrap_content" />   
  10.           
  11.     <RelativeLayout    
  12.         android:layout_width="fill_parent"    
  13.         android:layout_height="wrap_content"   
  14.         android:orientation="vertical" >    
  15.     
  16.         <LinearLayout    
  17.             android:id="@+id/viewGroup"    
  18.             android:layout_width="fill_parent"    
  19.             android:layout_height="wrap_content"    
  20.             android:layout_alignParentBottom="true"   
  21.             android:layout_marginBottom="30dp"    
  22.             android:gravity="center_horizontal"    
  23.             android:orientation="horizontal" >    
  24.         </LinearLayout>    
  25.     </RelativeLayout>    
  26.   
  27. </FrameLayout>  

使用ViewPager首先需要引入android-support-v4.jar这个jar包。自己不要忘记加

 

[java] view plaincopy
 
  1. package com.example.viewpagerdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.support.v4.view.PagerAdapter;  
  6. import android.support.v4.view.ViewPager;  
  7. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.widget.ImageView;  
  12. import android.widget.LinearLayout;  
  13.   
  14. public class TwoActivity extends Activity implements OnPageChangeListener{  
  15.     /** 
  16.      * ViewPager 
  17.      */  
  18.     private ViewPager viewPager;  
  19.       
  20.     /** 
  21.      * 装点点的ImageView数组 
  22.      */  
  23.     private ImageView[] tips;  
  24.       
  25.     /** 
  26.      * 装ImageView数组 
  27.      */  
  28.     private ImageView[] mImageViews;  
  29.       
  30.     /** 
  31.      * 图片资源id 
  32.      */  
  33.     private int[] imgIdArray ;  
  34.   
  35.     @Override  
  36.     protected void onCreate(Bundle savedInstanceState) {  
  37.         super.onCreate(savedInstanceState);  
  38.         setContentView(R.layout.activity_main);  
  39.         ViewGroup group = (ViewGroup)findViewById(R.id.viewGroup);  
  40.         viewPager = (ViewPager) findViewById(R.id.viewPager);  
  41.           
  42.         //载入图片资源ID  
  43.         imgIdArray = new int[]{R.drawable.item01, R.drawable.item02, R.drawable.item03, R.drawable.item04,  
  44.                 R.drawable.item05,R.drawable.item06, R.drawable.item07, R.drawable.item08};  
  45.           
  46.           
  47.         //将点点加入到ViewGroup中  
  48.         tips = new ImageView[imgIdArray.length];  
  49.         for(int i=0; i<tips.length; i++){  
  50.             ImageView imageView = new ImageView(this);  
  51.             imageView.setLayoutParams(new LayoutParams(10,10));  
  52.             tips[i] = imageView;  
  53.             if(i == 0){  
  54.                 tips[i].setBackgroundResource(R.drawable.page_indicator_focused);  
  55.             }else{  
  56.                 tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);  
  57.             }  
  58.               
  59.             LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,    
  60.                     LayoutParams.WRAP_CONTENT));  
  61.             layoutParams.leftMargin = 5;  
  62.             layoutParams.rightMargin = 5;  
  63.             group.addView(imageView, layoutParams);  
  64.         }  
  65.           
  66.           
  67.         //将图片装载到数组中  
  68.         mImageViews = new ImageView[imgIdArray.length];  
  69.         for(int i=0; i<mImageViews.length; i++){  
  70.             ImageView imageView = new ImageView(this);  
  71.             mImageViews[i] = imageView;  
  72.             imageView.setBackgroundResource(imgIdArray[i]);  
  73.         }  
  74.           
  75.         //设置Adapter  
  76.         viewPager.setAdapter(new MyAdapter());  
  77.         //设置监听,主要是设置点点的背景  
  78.         viewPager.setOnPageChangeListener(this);  
  79.         //设置ViewPager的默认项, 设置为长度的100倍,这样子开始就能往左滑动  
  80.         viewPager.setCurrentItem((mImageViews.length) * 100);  
  81.           
  82.     }  
  83.       
  84.     /** 
  85.      *  
  86.      * @author xiaanming 
  87.      * 
  88.      */  
  89.     public class MyAdapter extends PagerAdapter{  
  90.   
  91.         @Override  
  92.         public int getCount() {  
  93.             return Integer.MAX_VALUE;  
  94.         }  
  95.   
  96.         @Override  
  97.         public boolean isViewFromObject(View arg0, Object arg1) {  
  98.             return arg0 == arg1;  
  99.         }  
  100.   
  101.         @Override  
  102.         public void destroyItem(View container, int position, Object object) {  
  103.             ((ViewPager)container).removeView(mImageViews[position % mImageViews.length]);  
  104.               
  105.         }  
  106.   
  107.         /** 
  108.          * 载入图片进去,用当前的position 除以 图片数组长度取余数是关键 
  109.          */  
  110.         @Override  
  111.         public Object instantiateItem(View container, int position) {  
  112.             ((ViewPager)container).addView(mImageViews[position % mImageViews.length], 0);  
  113.             return mImageViews[position % mImageViews.length];  
  114.         }  
  115.           
  116.           
  117.           
  118.     }  
  119.   
  120.     @Override  
  121.     public void onPageScrollStateChanged(int arg0) {  
  122.           
  123.     }  
  124.   
  125.     @Override  
  126.     public void onPageScrolled(int arg0, float arg1, int arg2) {  
  127.           
  128.     }  
  129.   
  130.     @Override  
  131.     public void onPageSelected(int arg0) {  
  132.         setImageBackground(arg0 % mImageViews.length);  
  133.     }  
  134.       
  135.     /** 
  136.      * 设置选中的tip的背景 
  137.      * @param selectItems 
  138.      */  
  139.     private void setImageBackground(int selectItems){  
  140.         for(int i=0; i<tips.length; i++){  
  141.             if(i == selectItems){  
  142.                 tips[i].setBackgroundResource(R.drawable.page_indicator_focused);  
  143.             }else{  
  144.                 tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);  
  145.             }  
  146.         }  
  147.     }  
  148.   
  149. }  

 

代码下载

 

上面的代码中,当只有3张图片或者2张图片的时候,滑动存在BUG问题的修改如下

 

  • destroyItem(View container, int position, Object object)方法中不removeView

 

 

[java] view plaincopy
 
  1.     @Override  
  2.         public void destroyItem(View container, int position, Object object) {  
  3. //          ((ViewPager)container).removeView(mImageViews[position % mImageViews.length]);  
  4.         }  

 

  • instantiateItem(View container, int position)增加异常扑捉

 

 

[java] view plaincopy
 
  1. @Override  
  2.     public Object instantiateItem(View container, int position) {  
  3.         try {    
  4.             ((ViewPager)container).addView(mImageViews[position % mImageViews.length], 0);  
  5.         }catch(Exception e){  
  6.             //handler something  
  7.         }  
  8.         return mImageViews[position % mImageViews.length];  
  9.     }  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。