首页 > 代码库 > ViewPager左右轮滑

ViewPager左右轮滑

  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. }
  150. ViewPageAdapter
  151. package com.tree.schoollife.adapter;

    import android.support.v4.view.PagerAdapter;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.ViewParent;
    import android.widget.ImageView;

    import java.util.List;

    /**
    * Created by tree on 2016/11/10.
    */
    public class ViewPagerAdapter extends PagerAdapter {

    private List<ImageView> mData;

    public ViewPagerAdapter(List<ImageView> mData) {
    this.mData = http://www.mamicode.com/mData;
    }


    @Override
    public int getCount() {
    return Integer.MAX_VALUE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    //对ViewPager页号求模取出View列表中要显示的项
    position %= mData.size();
    if (position<0){
    position = mData.size()+position;
    }
    ImageView view = mData.get(position);
    //如果View已经在之前添加到了一个父组件,则必须先remove,否则会抛出IllegalStateException。
    ViewParent vp =view.getParent();
    if (vp!=null){
    ViewGroup parent = (ViewGroup)vp;
    parent.removeView(view);
    }
    container.addView(view);
    //add listeners here if necessary
    return view;


    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    //Warning:不要在这里调用removeView
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
    return view==object;
    }


    }
     

ViewPager左右轮滑