• ViewPager 无线循环并自动滚动


    Activity:

    private void initView(View view){
    
        initData();
    
        homeViewPagerAdapter = new HomeViewPagerAdapter(list);
        viewPager.setAdapter(homeViewPagerAdapter);
        startAutoPlay();
    }
    
    private void initData() {
        list = new ArrayList<>();
        ImageView view1 = new ImageView(getActivity());
        view1.setImageDrawable(getResources().getDrawable(R.mipmap.view_page));
        ImageView view2 = new ImageView(getActivity());
        view2.setImageDrawable(getResources().getDrawable(R.mipmap.home2));
        view2.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ImageView view3 = new ImageView(getActivity());
        view3.setImageDrawable(getResources().getDrawable(R.mipmap.home1));
        view3.setScaleType(ImageView.ScaleType.CENTER_CROP);
        list.add(view1);
        list.add(view2);
        list.add(view3);
    }
    
    /**
     *   开启自动滚动的方法
     */
    private void startAutoPlay() {
        isAutoPlay = true;
        handler.removeCallbacks(task);
        handler.postDelayed(task, delayTime);
    }
    
    /**
     * 暂停自动滚动
     */
    public void pauseAutoPlay() {
        isAutoPlay = false;
        handler.removeCallbacks(task);
    }
    /**
     * 自动滚动核心代码
     */
    private final Runnable task = new Runnable() {
        @Override
        public void run() {
            if (isAutoPlay) {
                // 自动滚动逻辑处理
                currentItem = viewPager.getCurrentItem();
                currentItem++;
                if(currentItem == homeViewPagerAdapter.getCount()) {
                    currentItem = 0;
                    viewPager.setCurrentItem(currentItem);
                    handler.postDelayed(this, delayTime);
                } else {
                    viewPager.setCurrentItem(currentItem);
                    handler.postDelayed(this, delayTime);
                }
            } else {
                handler.postDelayed(this, delayTime);
            }
        }
    };
    
    private Runnable runable = new Runnable() {
        @Override
        public void run() {
            //true表示平滑滚动
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
        }
    };
    public void startScroll() {
        timerTask = new TimerTask() {
            @Override
            public void run() {
                hander.post(runable);
            }
        };
        timer.schedule(timerTask, DISPLAY_TIME, DISPLAY_TIME);
    }
    public void stopScroll() {
        timerTask.cancel();
        timerTask = null;
    }
    

      

      

    HomeViewPagerAdapter:

    public class HomeViewPagerAdapter extends PagerAdapter {
    
        private List<ImageView> list;
    
        public HomeViewPagerAdapter(List<ImageView> list) {
            this.list = list;
        }
    
        @Override
        public int getCount() {
            return list.size();
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }
    
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }
    }
    

      

    主要逻辑就是使用Handler的postDelay()方法,不断得到viewpager的当前页,然后+1,再用viewpager的setCurrent()方法设置当前+1后的界面。

  • 相关阅读:
    【事件类】雅思口语
    【人物类】雅思口语
    【物品类】雅思口语
    Cassandra (二)Java | scala操作
    rpm安装在哪里了?
    用IDEA创建springboot项目遇到的问题Plugin 'org.springframework.boot:spring-boot-maven-plugin:' not found
    JDBC连接mysql出现The server time zone value '�й���׼ʱ��' is unrecognized
    Java反射、注解
    Junit单元测试
    Java-线程池、匿名内部类、Lambda表达式
  • 原文地址:https://www.cnblogs.com/IT-lss/p/9565605.html
Copyright © 2020-2023  润新知