利用定時器,實現循環輪播,很簡單;只需在定時器的消息里加以下代碼便可:ide
int count = adapter.getCount(); if (count > 1) { // 多於1個,才循環 int index = viewPager.getCurrentItem(); index = (index + 1) % count; viewPager.setCurrentItem(index, true); }
但如何在手指撥動時,也支持左右無限循環的功能呢?這個問題,我折騰了很久,網上資料多不可用。
比較好笑的是在適配器裏,設置數量爲最大值;而後,在當前位置爲中間的某個值,好比:動畫
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)container).removeView(mImageViews[position % count]); } public Object instantiateItem(View container, int position) { View view = mImageViews[position % count]; ((ViewPager)container).addView(view, 0); return view; } } protected void onCreate(Bundle savedInstanceState) { viewPager.setAdapter(new MyAdapter()); //設置ViewPager的默認項, 設置爲總數的100倍,一開始才能往左滑動 viewPager.setCurrentItem(count * 100); }
這種作法,實際上,並無真正達到左右無限循環;只是通常狀況下,很難達到邊界(不可滑動)的狀況。
經過查閱大量的資料,我發現ViewPager自身並不支持左右無限循環的功能,不得不說,這點真是太失敗了。
因爲ViewPager自身不支持,咱們只能本身找方法了!下面是我實現左右無限循環的方法:
用於顯示的mViews,比數據源mList,多了兩個節點元素(頭節點0:b和尾節點5:e用於跳轉)
下圖的不帶箭頭的紅線,是mViews根據mList初始化的狀況;帶箭頭的紅線是跳轉的狀況。spa
具體代碼以下:code
public class AdapterCycle extends PagerAdapter implements ViewPager.OnPageChangeListener{ private Context mContext; // 上下文 private LayoutInflater mInflater; // 用於解XML private LinkedList<View> mViews; // <span style="font-family: Arial, Helvetica, sans-serif;">用於</span><span style="font-family: Arial, Helvetica, sans-serif;">顯示的View</span> private List<Drawable> mList; // 數據源<span style="font-family: Arial, Helvetica, sans-serif;">Drawable</span> private ViewPager mViewPager; //頁面 public AdapterCycle(Context context, ViewPager viewPager, List<Drawable> list) { mContext = context; mInflater = LayoutInflater.from(context); mViewPager = viewPager; if (list != null) { //不管是否多於1個,都要初始化第一個(index:0) mViews = new LinkedList<View>(); ImageView view = (ImageView) mInflater.inflate( R.layout.activity_main_item_cycle, null); Drawable drawable = list.get(list.size() - 1); view.setImageDrawable(drawable); mViews.add(view); //注意,若是不僅1個,mViews比mList多兩個(頭尾各多一個) //假設:mList爲mList[0~N-1], mViews爲mViews[0~N+1] // mViews[0]放mList[N-1], mViews[i]放mList[i-1], mViews[N+1]放mList[0] // mViews[1~N]用於循環;首位以前的mViews[0]和末尾以後的mViews[N+1]用於跳轉 // 首位以前的mViews[0],跳轉到末尾(N);末位以後的mViews[N+1],跳轉到首位(1) if( list.size() > 1) { //多於1個要循環 for (Drawable d : list) { //中間的N個(index:1~N) ImageView v = (ImageView) mInflater.inflate( R.layout.activity_main_item_cycle, null); v.setImageDrawable(d); mViews.add(v); } //最後一個(index:N+1) view = (ImageView) mInflater.inflate( R.layout.activity_main_item_cycle, null); drawable = dList.get(0); view.setImageDrawable(drawable); mViews.add(view); } } } @Override public int getCount() { return mViews.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView(mViews.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { View view = mViews.get(position); container.addView(view); return view; } // 實現ViewPager.OnPageChangeListener接口 @Override public void onPageSelected(int position) { LogUtils.logi(TAG, "onPageSelected:" + position); if ( mViews.size() > 1) { //多於1,纔會循環跳轉 if ( position < 1) { //首位以前,跳轉到末尾(N) position = mList.size(); //注意這裏是mList,而不是mViews mViewPager.setCurrentItem(position, false); } else if ( position > mList.size()) { //末位以後,跳轉到首位(1) mViewPager.setCurrentItem(1, false); //false:不顯示跳轉過程的動畫 position = 1; } } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // 什麼都不幹 } @Override public void onPageScrollStateChanged(int state) { // 什麼都不幹 } } //在外層,將mViewPager初始位置設置爲1便可 if (mAdapterCycle.getCount() > 1) { //多於1個,才循環並開啓定時器 mViewPager.setCurrentItem(1); //將mViewPager初始位置設置爲1 startTimer(); //開啓定時器,定時切換頁面 }
這時,利用定時器,實現循環輪播,須要修改爲以下:blog
int count = adapter.getCount(); if (count > 2) { // 實際上,多於1個,就多於3個 int index = viewPager.getCurrentItem(); index = index % (count - 2) + 1; //這裏修改過 viewPager.setCurrentItem(index, true); }