上一张效果图:
之前的项目有一个Galley的项目,但是代码结构特别乱,别问我为什么,我也是刚接手这个项目,为了方便以后阅读和维护我对一些模块进行了重构。ViewPager实现Galler效果,但是当时时间比较急,写的比较仓促,上一篇实现了简单的效果,但是对于初始的时候左边滑动是有问题的,这是因为我们在自己的Adapter的时候对于getCount,我们想通过Integer.MAX_VALUE来实现无限循环,简单的说这个是向右(无限大)吧,所以也就只是实现了向右的循环,对于像左的还是不行的。
那么怎么解决呢?
首先我们对Adapter还是需要按以前的写法,其实Adapter适配器就是帮我们做界面的,其他的逻辑最好不要加,我之前的想法是用过
viewPager.addOnPageChangeListener来实现,通过判断是否滑到最左边或者最右边来实现循环滑到。
- int realPosition = mAdapter.toRealPosition(position);
- if (mPreviousPosition != realPosition) {
- mPreviousPosition = realPosition;
- if (mOuterPageChangeListener != null) {
- mOuterPageChangeListener.onPageSelected(realPosition);
- }
- }
这里我封装了一个类,只要将你的ViewPager改为我封装好的ViewPager就行LoopViewPager
- public class LoopViewPager extends ViewPager {
- private static final boolean DEFAULT_BOUNDARY_CASHING = false;
- private OnPageChangeListener mOuterPageChangeListener;
- private LoopPagerAdapterWrapper mAdapter;
- private boolean mBoundaryCaching = DEFAULT_BOUNDARY_CASHING;
- public static int toRealPosition( int position, int count ){
- position = position-1;
- if( position < 0 ){
- position += count;
- }else{
- position = position%count;
- }
- return position;
- }
- public void setBoundaryCaching(boolean flag) {
- mBoundaryCaching = flag;
- if (mAdapter != null) {
- mAdapter.setBoundaryCaching(flag);
- }
- }
- @Override
- public void setAdapter(PagerAdapter adapter) {
- mAdapter = new LoopPagerAdapterWrapper(adapter);
- mAdapter.setBoundaryCaching(mBoundaryCaching);
- super.setAdapter(mAdapter);
- setCurrentItem(0, false);
- }
- @Override
- public PagerAdapter getAdapter() {
- return mAdapter != null ? mAdapter.getRealAdapter() : mAdapter;
- }
- @Override
- public int getCurrentItem() {
- return mAdapter != null ? mAdapter.toRealPosition(super.getCurrentItem()) : 0;
- }
- public void setCurrentItem(int item, boolean smoothScroll) {
- int realItem = mAdapter.toInnerPosition(item);
- super.setCurrentItem(realItem, smoothScroll);
- }
- @Override
- public void setCurrentItem(int item) {
- if (getCurrentItem() != item) {
- setCurrentItem(item, true);
- }
- }
- @Override
- public void setOnPageChangeListener(OnPageChangeListener listener) {
- mOuterPageChangeListener = listener;
- };
- public LoopViewPager(Context context) {
- super(context);
- init();
- }
- public LoopViewPager(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- private void init() {
- super.setOnPageChangeListener(onPageChangeListener);
- }
- private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
- private float mPreviousOffset = -1;
- private float mPreviousPosition = -1;
- @Override
- public void onPageSelected(int position) {
- int realPosition = mAdapter.toRealPosition(position);
- if (mPreviousPosition != realPosition) {
- mPreviousPosition = realPosition;
- if (mOuterPageChangeListener != null) {
- mOuterPageChangeListener.onPageSelected(realPosition);
- }
- }
- }
- @Override
- public void onPageScrolled(int position, float positionOffset,
- int positionOffsetPixels) {
- int realPosition = position