...
private void processData(String json) { ...... //發消息每隔4000切換一次ViewPager頁面 if(internalHandler == null){ internalHandler = new InternalHandler(); } //是把消息隊列所有的消息和回調移除 internalHandler.removeCallbacksAndMessages(null); internalHandler.postDelayed(new MyRunnable(),4000); }
class MyOnPageChangeListener implements ViewPager.OnPageChangeListener { ..... private boolean isDragging = false; @Override public void onPageScrollStateChanged(int state) { if(state ==ViewPager.SCROLL_STATE_DRAGGING){//拖拽 isDragging = true; LogUtil.e("拖拽"); //拖拽要移除消息 internalHandler.removeCallbacksAndMessages(null); }else if(state ==ViewPager.SCROLL_STATE_SETTLING&&isDragging){//慣性 //發消息 LogUtil.e("慣性"); isDragging = false; internalHandler.removeCallbacksAndMessages(null); internalHandler.postDelayed(new MyRunnable(),4000); }else if(state ==ViewPager.SCROLL_STATE_IDLE&&isDragging){//靜止狀態 //發消息 LogUtil.e("靜止狀態"); isDragging = false; internalHandler.removeCallbacksAndMessages(null); internalHandler.postDelayed(new MyRunnable(),4000); } } }