2015-10-07 78 views
1

頁我在ViewPager 4張圖片,我想一個一個滑動這些圖像和第四圖像後,我想說明第一image.following代碼顯示了自動滑動翻頁,但在第四張照片後停止。如何ViewPager自動幻燈片的Android

  switchtimer= new Timer(); 
      switchtimer.schedule(new TimerTask() { 

       @Override 
       public void run() { 
        Log.v("TimerMtd", "Timer call"); 
        handler.post(Update); 
       } 
      }, 1500,3000); 
     } 
    Handler handler = new Handler(); 

    Runnable Update = new Runnable() { 
     public void run() { 
      mPager.setCurrentItem(currentpage); 
      currentpage++; 
      if(currentpage > 2){ 
       currentpage = 0; 
       Log.v("TimerMtd", currentpage+"cutrrentpage ="); 
      } 

      } 
    }; 
+0

PLZ建議如果有任何替代 –

+0

嗨,Patle,你好嗎? –

+0

KYA patle嚳回覆德迪亞KAR瓊脂你們TU海來。從圖姆薩爾喜海吶 –

回答

1

我已經取得了一些方法來從一個做的一樣,得到了refrence SO回答,但沒有記住的是,現在,下面是我使用的代碼:

Timer timer; 
int page = 0; 

public void pageSwitcher(int seconds) { 
    timer = new Timer(); // At this line a new Thread will be created 
    timer.scheduleAtFixedRate(new RemindTask(), 0, seconds * 1000); // delay 
    // in 
    // milliseconds 
} 

// this is an inner class... 
class RemindTask extends TimerTask { 

    @Override 
    public void run() { 

     // As the TimerTask run on a seprate thread from UI thread we have 
     // to call runOnUiThread to do work on UI thread. 
     runOnUiThread(new Runnable() { 
      public void run() { 

       if (page > 5) { // In my case the number of pages are 5 
        page = 0; 
       } else { 
        viewPager.setCurrentItem(page); 
        page++; 
       } 
      } 
     }); 

    } 
} 

onCreate()我稱這種方法是這樣的

pageSwitcher(3); // Here "3" is delay in seconds