首页 > 其他 > 详细

无限循环,自动滚动,兼容PageIndicator的ViewPager

时间:2014-02-13 19:31:33      阅读:451      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
package cn.paly.bz.view;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * 顶部ViewPager 
 * 自动滚动以及无限循环滚动
 * @author Mr_wu
 * @mail 97046335@qq.com 
 */
public class HeadViewPager extends ViewPager implements Runnable {

    private PagerAdapter pagerAdapter;

    private static final int POST_DELAYED_TIME = 1000 * 5;

    public HeadViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        postDelayed(this, POST_DELAYED_TIME);
    }

    // 对setAdapter的数据进行包装
    private class MyPagerAdapter extends PagerAdapter {
        private PagerAdapter pa;

        public MyPagerAdapter(PagerAdapter pa) {
            this.pa = pa;
        }

        @Override
        // 关键之一:修改Count长度
        public int getCount() {
            return pa.getCount() > 1 ? pa.getCount() + 2 : pa.getCount();
        }

        @Override
        // 这里是关键之二:修改索引(如果不考虑内容问题可以全部加载进数组然后操作更简单)
        public Object instantiateItem(ViewGroup container, int position) {
            if (position == 0) {
                return pa.instantiateItem(container, pa.getCount() - 1);
            } else if (position == pa.getCount() + 1) {
                return pa.instantiateItem(container, 0);
            } else {
                return pa.instantiateItem(container, position - 1);
            }
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            pa.destroyItem(container, position, object);
        }

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

    // 包装setOnPageChangeListener的数据
    private class MyOnPageChangeListener implements OnPageChangeListener {
        private OnPageChangeListener listener;

        public MyOnPageChangeListener(OnPageChangeListener listener) {
            this.listener = listener;
        }

        @Override
        // 关键之三:
        public void onPageScrollStateChanged(int arg0) {
            if (arg0 == SCROLL_STATE_IDLE) {
                if (getCurrentItem() + 1 == 0) {
                    setCurrentItem(pagerAdapter.getCount() - 1, false);
                } else if (getCurrentItem() + 1 == pagerAdapter.getCount() + 1) {
                    setCurrentItem(0, false);
                }
            }

            listener.onPageScrollStateChanged(arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            listener.onPageScrolled(arg0, arg1, arg2);
        }

        @Override
        // 关键四:
        public void onPageSelected(int arg0) {
            if (arg0 == 0) {
                //
            } else if (arg0 == pagerAdapter.getCount() + 1) {
                //
            } else {
                listener.onPageSelected(arg0 - 1);
            }

        }

    }

    @Override
    public void setOnPageChangeListener(OnPageChangeListener listener) {
        super.setOnPageChangeListener(listener == null ? null : new MyOnPageChangeListener(listener));
    }

    @Override
    public void setAdapter(PagerAdapter arg0) {
        super.setAdapter(arg0 == null ? null : new MyPagerAdapter(arg0));

        if (arg0 != null && arg0.getCount() != 0) {
            setCurrentItem(0, false);
        }
        this.pagerAdapter = arg0;
    }

    @Override
    // 兼容PageIndicator
    public PagerAdapter getAdapter() {
        return pagerAdapter;
    }

    @Override
    public int getCurrentItem() {
        return super.getCurrentItem() - 1;
    }

    @Override
    public void setCurrentItem(int item) {
        setCurrentItem(item, true);
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        super.setCurrentItem(item + 1, smoothScroll);
    }

    @Override
    // 自动滚动关键
    public void run() {
        if (getAdapter().getCount() > 1) {
            setCurrentItem(getCurrentItem() + 1, true);
        }
        postDelayed(this, POST_DELAYED_TIME);
    }

}
bubuko.com,布布扣

详细原理参见:http://stackoverflow.com/a/12965787

无限循环,自动滚动,兼容PageIndicator的ViewPager

原文:http://www.cnblogs.com/moqi2013/p/3547816.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!