Android使用ViewPager做无限轮播,人为滑动时停止

实现无限循环

使用取模,获取到当前图片所在的位置。

    private class MyPagerAdapter extends PagerAdapter {
        private Context context;
        List list;

        public MyPagerAdapter(Context context, List list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            //使用Integer的最大值,这样基本上就不需要滑动到最后了
            //除非用户很蛋疼
            return Integer.MAX_VALUE;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //当前位置模上总数等于图片在数组中的位置
            position %= list.size();
            if (position < 0) {
                position = list.size() + position;
            }
            ImageView imageView = list.get(position);
            //设置轮播点击事件
            final int finalPosition = position;
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), BannerActivity.class);
                    intent.putExtra("position", finalPosition);
                    startActivity(intent);
                }
            });
            ViewGroup parent = (ViewGroup) imageView.getParent();
            if (parent != null) {
                parent.removeView(imageView);
            }
            container.addView(imageView);
            return list.get(position);
        }

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

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //注意这里不要使用
            //container.removeView(list.get(position));
        }
    }

自动循环实现

使用Handler每隔5秒一次消息,接收到轮播的动作就继续发一次消息,实现自动轮播的效果。

    private class MyHandler extends Handler {
        private Context context;
        //使用弱引用,避免handler造成内存泄露
        private WeakReference weakReference;

        public MyHandler(WeakReference weakReference) {
            this.weakReference = weakReference;
        }


        @Override
        public void handleMessage(Message msg) {
            MainActivity activity = weakReference.get();
            //Activity不存在了,就不需要再处理了
            if (activity == null) {
                return;
            }
            //如果已经有消息了,先移除消息
            if (activity.handler.hasMessages(BANNER_NEXT)) {
                activity.handler.removeMessages(BANNER_NEXT);
            }
            switch (msg.what) {
                case BANNER_NEXT:
                    //跳到下一页,
                    int currentItem = vpBanner.getCurrentItem();
                    activity.vpBanner.setCurrentItem(++currentItem);
                    //5秒后继续轮播
                    activity.handler.sendEmptyMessageDelayed(BANNER_NEXT, 5000);
                    break;
                case BANNER_PAUSE:
                    //暂停,不需要做任务操作
                    break;
                case BANNER_RESUME:
                    //继续轮播
                    activity.handler.sendEmptyMessageDelayed(BANNER_NEXT, 5000);
                    break;
            }
        }
    }

在Activity中使用

        //这里放了3张图片,使用Gilde加载
        ImageView imageView1 = new ImageView(this);
        ImageView imageView2 = new ImageView(this);
        ImageView imageView3 = new ImageView(this);
        RequestManager requestManager = Glide.with(this);
        requestManager.load("http://img1.imgtn.bdimg.com/it/u=3251910782,1174316217&fm=21&gp=0.jpg").into(imageView1);
        requestManager.load("http://pic2.ooopic.com/10/78/50/34b1OOOPICf0.jpg").into(imageView2);
        requestManager.load("http://www.swkweike.com/data/uploads/2015/08/18/193226787255d2dbd711a33.jpg").into(imageView3);
        List list = new ArrayList<>();
        list.add(imageView1);
        list.add(imageView2);
        list.add(imageView3);
        MyPagerAdapter adapter = new MyPagerAdapter(this, list);
        vpBanner = (ViewPager) findViewById(R.id.vpBanner);
        vpBanner.setAdapter(adapter);
        //将起始位置设置在中间,让用户看不到边界
        vpBanner.setCurrentItem(Integer.MAX_VALUE / 2);
        //开始轮播
        handler.sendEmptyMessageDelayed(BANNER_NEXT, 5000);

用户滑动时停止轮播,松开时继续轮播

使用ViewPager的滑动监听事件,判断用户是否正在滑动

        vpBanner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                switch (state) {
                    case ViewPager.SCROLL_STATE_DRAGGING:
                        //用户正在滑动,暂停轮播
                        handler.sendEmptyMessage(BANNER_PAUSE);
                        break;
                    case ViewPager.SCROLL_STATE_IDLE:
                        //滑动结束,继续轮播
                        handler.sendEmptyMessageDelayed(BANNER_NEXT, 5000);
                        break;
                }

            }
        });

你可能感兴趣的:(Android学习,android,轮播图,viewpager,无限轮播)