1. 定时切换
2. 切换图片
3. 切换标题
4. 切换指示器
定时切换用到一个类:ScheduledExecutorService
作用是定时执行任务,我们这里要做的定时任务是,2秒执行一次图片切换
private void startAd() { scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); // 当Activity显示出来后,每两秒切换一次图片显示 scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 1, 2, TimeUnit.SECONDS); }
定义子线程
private class ScrollTask implements Runnable { @Override public void run() { synchronized (adViewPager) { currentItem = (currentItem + 1) % imageViews.size(); handler.obtainMessage().sendToTarget(); } } }
通过handle来通知ViewPager进行视图切换
private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { adViewPager.setCurrentItem(currentItem); }; };