美文网首页
ViewPager无限轮播

ViewPager无限轮播

作者: 我爱吃青苹果 | 来源:发表于2017-05-03 22:43 被阅读0次

原理

  • getCount()方法获取100万条的数据
  • 利用取余数的运算,获取有效数据
  • 手动将开始条目设置为中间值(50万)
//viewpager容器类
public class ViewPagerHolder {
    private  Context mContext;
    private  ViewPager mViewPager;//viewpager
    private  ViewPagerAdapter mAdapter;//适配器
    private  ShowNextPage mShowNextPage;//轮播器
    private   Handler mHandler;

    public ViewPagerHolder(Context context,ViewPager viewPager) {
        this.mContext = context;
        this.mViewPager = viewPager;
        mHandler = new Handler();
    }

    /**
     * 初始化数据
     * @param datas
     */
    public void initData(List<Integer> datas) {
        mAdapter = new ViewPagerAdapter(mContext, datas);
        mViewPager.setAdapter(mAdapter);
        
        toNextPage();
    }

    /**
     * 开始无限轮播
     */
    private void toNextPage() {
        //设置当前页面
        mViewPager.setCurrentItem(mAdapter.getCount() / 2);

        mShowNextPage = new ShowNextPage();

        //开始轮播
        mShowNextPage.start();

        //触摸\滑动不轮播
        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //按下不轮播
                        mShowNextPage.stop();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        //移动不轮播
                        mShowNextPage.stop();
                        break;
                    case MotionEvent.ACTION_UP:
                        //抬起轮播
                        mShowNextPage.start();
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        //取消操作,轮播
                        mShowNextPage.start();
                        break;
                }
                return false;
            }
        });

    }

    /**
     * 轮播显示下一个页面
     */
    class ShowNextPage implements Runnable {
        //开始
        public void start() {
            mHandler.removeCallbacks(this);
            mHandler.postDelayed(this, 3000);
        }

        //停止
        public void stop() {
            mHandler.removeCallbacksAndMessages(null);
        }

        @Override
        public void run() {
            int currentItem = mViewPager.getCurrentItem();
            mViewPager.setCurrentItem(currentItem + 1);
            mHandler.postDelayed(this, 3000);
        }
    }

    class ViewPagerAdapter extends PagerAdapter {
        Context mContext;
        List<Integer> datas;

        public ViewPagerAdapter(Context context, List<Integer> datas) {
            mContext = context;
            this.datas = datas;
        }

        @Override
        public int getCount() {
            //100万条数据
            return datas.size() * 1000 * 1000;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //取余数,保证为有效数据
            position = position % datas.size();

            int viewId = datas.get(position);
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(viewId);
            imageView.setScaleType(ImageView.ScaleType.CENTER);

            //添加到父容器
            container.addView(imageView);

            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //移除
            container.removeView((View) object);
        }
    }
}

相关文章

网友评论

      本文标题:ViewPager无限轮播

      本文链接:https://www.haomeiwen.com/subject/fdgltxtx.html