ViewPager的简单优化和实现大图轮播

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bull_man/article/details/53144733

大图轮播的实现方式就是通过ViewPager实现,然后定时改变ViewPager的setCurrentItem。虽然ViewPager加载几张图片不会导致程序崩溃,但是能优化的地方最好还是优化

代码

public class HomePictureHolder extends BaseHolder<List<String>> {


    private ViewPager viewPager;
    private List<String> datas;

    @Override
    protected void refreshView(List<String> datas) {
        this.datas = datas;
        viewPager.setAdapter(new HomePictureAdapter());
        viewPager.setCurrentItem(1000 * datas.size());

        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        auToRunTask.stop();
                        break;
                    // 事件的取消,即在viewpager按下,不在viewpager松开,会导致不在viewpager松开,viewpager不会自动轮播
                    case MotionEvent.ACTION_CANCEL:
                        auToRunTask.start();
                        break;
                    case MotionEvent.ACTION_UP:
                        auToRunTask.start();
                        break;
                }
                return false;
            }
        });
        auToRunTask = new AuToRunTask();
        auToRunTask.start();
    }

    @Override
    protected View initView() {
        viewPager = new ViewPager(UiUtils.getContext());
        viewPager.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
                (int) UiUtils.getResource().getDimension(R.dimen.itme_app_height)));

        return viewPager;
    }

    private boolean flag;
    private AuToRunTask auToRunTask;

    public class AuToRunTask implements Runnable {
        @Override
        public void run() {
            if (flag) {
                UiUtils.cancel(this);  // 取消之前
                int currentItem = viewPager.getCurrentItem();

                viewPager.setCurrentItem(++currentItem);
                //  延迟执行当前的任务
                UiUtils.postDelayed(this,5000);
            }
        }

        /* 开始自动轮播*/
        public void start(){
            if(!flag){
                UiUtils.cancel(this);
                flag = true;
                UiUtils.postDelayed(this,5000);
            }
        }
        /* 停止自动轮播*/
        public void stop(){
            if(flag){
                UiUtils.cancel(this);
                flag = false;
            }
        }

    }


    class HomePictureAdapter extends PagerAdapter {
        //把ImagerVIew放到一个公共内存,以复用
        LinkedList<ImageView> convertView = new LinkedList<>();

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            int index = position % datas.size();
            ImageView view;
            if (convertView.size() > 0) {
                view = convertView.remove(0);//把ImageView从公共内存中取出
            } else {
                view = new ImageView(UiUtils.getContext());
            }
            bitmapUtils.display(view, HttpHelp.SERVER_URL + "image?name=" + datas.get(index));
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ImageView imageView = (ImageView) object;
            //viewPager移除View时,把ImagerVIew放到公共内存
            convertView.add(imageView);
            container.removeView(imageView);
        }
    }
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页