关于ViewPager的无限滑动

2017-01-04  本文已影响54人  匡风含情

一段很简单的代码,来实现ViewPager的无限滑动。一个很简单的小技巧~

private class VPAdapter extends PagerAdapter {

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

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            // try {
            // int index = position % list.size();
            // ViewPager vp = (ViewPager) container;
            // vp.removeView(list.get(index));
            // } catch (Exception e) {
            // e.printStackTrace();
            // }
        }

        @Override
        public Object instantiateItem(View container, int position) {
            int index = position % list.size();
            try {
                ViewPager vp = (ViewPager) container;
                vp.addView(list.get(index), 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list.get(index);
        }

    }
上一篇下一篇

猜你喜欢

热点阅读