實現ViewPager無限循環:ide
1.讓ViewPager的PagerAdapter無限大且內容重複oop
2.實現自動滾屏控制器post
上源碼:this
1 /** 2 * ViewPager無限循環實現 3 * 一、實現一個無限大的PagerAdapter 4 * 二、繼承ViewPager重寫setAdapter方法 5 * 三、實現一個自動滾屏的控制器 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 @Override 108 // 自動滾動關鍵 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 @Override 123 public boolean onTouchEvent(MotionEvent event) { 124 if (event.getAction() == MotionEvent.ACTION_DOWN) { 125 stopLoop(); 126 } else if (event.getAction() == MotionEvent.ACTION_UP 127 || event.getAction() == MotionEvent.ACTION_CANCEL) { 128 if (isLoop) { 129 startLoop(); 130 } 131 } 132 return super.onTouchEvent(event); 133 } 134 135 /** 136 * 無限大的pagerAdapter 137 * @author sdsw 138 * 139 */ 140 class LoopPagerAdapter extends PagerAdapter { 141 142 private PagerAdapter adapter; 143 144 public LoopPagerAdapter(PagerAdapter adapter) { 145 this.adapter = adapter; 146 } 147 148 @Override 149 public int getCount() { 150 // TODO Auto-generated method stub 151 return Integer.MAX_VALUE; 152 } 153 154 @Override 155 public boolean isViewFromObject(View arg0, Object arg1) { 156 // TODO Auto-generated method stub 157 return adapter.isViewFromObject(arg0, arg1); 158 } 159 160 @Override 161 public void destroyItem(ViewGroup container, int position, Object object) { 162 // TODO Auto-generated method stub 163 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 @Override 171 public Object instantiateItem(ViewGroup container, int position) { 172 // TODO Auto-generated method stub 173 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 @Override 181 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 }