首页 > 代码库 > ViewPager实现滑屏的无限循环

ViewPager实现滑屏的无限循环

实现ViewPager无限循环:

1.让ViewPager的PagerAdapter无限大且内容重复

2.实现自动滚屏控制器

上源码:

  1 /**  2  * ViewPager无限循环实现  3  * 1、实现一个无限大的PagerAdapter   4  * 2、继承ViewPager重写setAdapter方法  5  * 3、实现一个自动滚屏的控制器  6  * 注意:数据刷新调用LoopViewPager类里的notifyDataSetChanged   7  * @author sdsw  8  *  9  */ 10 public class LoopViewPager extends ViewPager implements Runnable { 11  12     //无限大的PagerAdapter 13     private LoopPagerAdapter loopAdapter; 14     private PagerAdapter adapter; 15  16     //自动滚屏时间间隔 17     private int POST_DELAYED_TIME = 2000; 18     //自动滚屏开关 19     private boolean isLoop = true; 20     //解决notifyDataSetChanged不刷新数据问题 21     private boolean isNotify = false; 22  23     public LoopViewPager(Context context, AttributeSet attrs) { 24         super(context, attrs); 25         // TODO Auto-generated constructor stub 26     } 27  28     @Override 29     public void setAdapter(PagerAdapter adapter) { 30         // TODO Auto-generated method stub 31         if (adapter != null) { 32             //修改PagerAdapter为无限大 33             loopAdapter = new LoopPagerAdapter(adapter); 34             this.adapter = adapter; 35         } 36         super.setAdapter(loopAdapter); 37         startLoop(); 38     } 39  40     @Override 41     public PagerAdapter getAdapter() { 42         // TODO Auto-generated method stub 43         return adapter; 44     } 45  46     public LoopPagerAdapter getLoopAdapter() { 47         // TODO Auto-generated method stub 48         return loopAdapter; 49     } 50  51     /** 52      * 解决数据刷新问题 53      */ 54     public void notifyDataSetChanged() { 55         isNotify = true; 56         loopAdapter.notifyDataSetChanged(); 57         if(isLoop) { 58             stopLoop(); 59             startLoop(); 60         } 61     } 62  63     /** 64      * 设置自动滚屏时间 65      * @param time 66      */ 67     public void setLoopTime(int time) { 68         POST_DELAYED_TIME = time; 69     } 70  71     /** 72      * 开启和关闭滚屏 73      * @param isLoop 74      */ 75     public void setLoop(boolean isLoop) { 76         this.isLoop = isLoop; 77         if (isLoop) { 78             startLoop(); 79         } else { 80             startLoop(); 81         } 82     } 83  84     private void startLoop() { 85         stopLoop(); 86         postDelayed(this, POST_DELAYED_TIME); 87     } 88  89     private void stopLoop() { 90         removeCallbacks(this); 91     } 92      93     /** 94      * 获取当前的item 95      * @return 96      */ 97     public int getLoopCurrentItem(){ 98         int curent_item = 0; 99         if(adapter != null && getCurrentItem() != 0 && adapter.getCount() != 0) {100             if(getCurrentItem() >= adapter.getCount()) {101                 curent_item = getCurrentItem() % adapter.getCount();102             }103         }104         return curent_item;105     }106 107     @Override108     // 自动滚动关键109     public void run() {110         if (!isLoop) {111             return;112         }113         if (getAdapter() != null && getAdapter().getCount() > 1) {114             setCurrentItem(getCurrentItem() + 1);115         }116         startLoop();117     }118 119     /**120      * 当touch时取消自动滚动121      */122     @Override123     public boolean onTouchEvent(MotionEvent event) {124         if (event.getAction() == MotionEvent.ACTION_DOWN) {125             stopLoop();126         } else if (event.getAction() == MotionEvent.ACTION_UP127                 || event.getAction() == MotionEvent.ACTION_CANCEL) {128             if (isLoop) {129                 startLoop();130             }131         }132         return super.onTouchEvent(event);133     }134 135     /**136      * 无限大的pagerAdapter137      * @author sdsw138      *139      */140     class LoopPagerAdapter extends PagerAdapter {141 142         private PagerAdapter adapter;143 144         public LoopPagerAdapter(PagerAdapter adapter) {145             this.adapter = adapter;146         }147 148         @Override149         public int getCount() {150             // TODO Auto-generated method stub151             return Integer.MAX_VALUE;152         }153 154         @Override155         public boolean isViewFromObject(View arg0, Object arg1) {156             // TODO Auto-generated method stub157             return adapter.isViewFromObject(arg0, arg1);158         }159 160         @Override161         public void destroyItem(ViewGroup container, int position, Object object) {162             // TODO Auto-generated method stub163             int count = adapter.getCount();164             int new_position = 0;165             if (count != 0)166                 new_position = position % adapter.getCount();167             adapter.destroyItem(container, new_position, object);168         }169 170         @Override171         public Object instantiateItem(ViewGroup container, int position) {172             // TODO Auto-generated method stub173             int count = adapter.getCount();174             int new_position = 0;175             if (count != 0)176                 new_position = position % adapter.getCount();177             return adapter.instantiateItem(container, new_position);178         }179 180         @Override181         public int getItemPosition(Object object) {182             if (isNotify) {183                 isNotify = false;184                 return POSITION_NONE;185             }186             return super.getItemPosition(object);187         }188     }189 190 }

 

ViewPager实现滑屏的无限循环