- int count = adapter.getCount();
- if (count > 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.setCurrentItem(count * 100);
- }
这种做法,实际上,并没有真正达到左右无限循环;只是一般情况下,很难达到边界(不可滑动)的情况。
通过查阅大量的资料,我发现ViewPager自身并不支持左右无限循环的功能,不得不说,这点真是太失败了。
由于ViewPager自身不支持,我们只能自己找方法了!下面是我实现左右无限循环的方法:
用于显示的mViews,比数据源mList,多了两个节点元素(头节点0:b和尾节点5:e用于跳转)
下图的不带箭头的红线,是mViews根据mList初始化的情况;带箭头的红线是跳转的情况。
具体代码如下:
- public class AdapterCycle extends PagerAdapter
- implements ViewPager.OnPageChangeListener{
- private Context mContext;
- private LayoutInflater mInflater;
- private LinkedList<View> mViews;
- private List<Drawable> mList;
- private ViewPager mViewPager;
-
-
- public AdapterCycle(Context context, ViewPager viewPager,
- List<Drawable> list)
- {
- mContext = context;
- mInflater = LayoutInflater.from(context);
- mViewPager = viewPager;
- if (list != null) {
-
- 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);
-
-
-
-
-
- if( list.size() > 1) {
- for (Drawable d : list) {
- ImageView v = (ImageView) mInflater.inflate(
- R.layout.activity_main_item_cycle, null);
- v.setImageDrawable(d);
- mViews.add(v);
- }
-
- 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;
- }
-
-
-
- @Override
- public void onPageSelected(int position) {
- LogUtils.logi(TAG, "onPageSelected:" + position);
- if ( mViews.size() > 1) {
- if ( position < 1) {
- position = mList.size();
- mViewPager.setCurrentItem(position, false);
- } else if ( position > mList.size()) {
- mViewPager.setCurrentItem(1, false);
- position = 1;
- }
- }
- }
-
- @Override
- public void onPageScrolled(int position, float positionOffset,
- int positionOffsetPixels) {
-
- }
-
- @Override
- public void onPageScrollStateChanged(int state) {
-
- }
- }
-
-
-
- if (mAdapterCycle.getCount() > 1) {
- mViewPager.setCurrentItem(1);
- startTimer();
- }
这时,利用定时器,实现循环轮播,需要修改成如下:
- int count = adapter.getCount();
- if (count > 2) {
- int index = viewPager.getCurrentItem();
- index = index % (count - 2) + 1;
- viewPager.setCurrentItem(index, true);
- }