2012-04-06 140 views
11

我已經使用android視圖分頁器來顯示圖像和文本現在我想要的是,如果用戶不更改圖像,那麼它應該開始自動更改圖像後幾秒,當用戶再次開始使用他的手指時,圖像不應該自動改變?幾秒鐘後Android視圖分頁器自動更改頁面

我試圖使用處理程序來創建延遲,但它沒有爲我工作?

請幫助任何人

public class SimpleImageViewerActivity extends Activity implements ViewPager.OnPageChangeListener{ 
    private Handler handler; 
    ViewPager myPager; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.imageslideshow); 
     MyPagerAdapter adapter = new MyPagerAdapter(); 
     myPager = (ViewPager) findViewById(R.id.slideShowPager); 
     myPager.setAdapter(adapter); 
     myPager.setCurrentItem(0); 

     handler = new Handler(); 
    myPager.setOnPageChangeListener(this); 
    } 

    private class MyPagerAdapter extends PagerAdapter { 

     public int getCount() { 
      return 5; 
     } 

     public Object instantiateItem(View collection, int position) { 

      LayoutInflater inflater = (LayoutInflater) collection.getContext() 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      int resId = 0; 
      switch (position) { 
      case 0: 
       resId = R.layout.farleft; 
       break; 
      case 1: 
       resId = R.layout.left; 
       break; 
      case 2: 
       resId = R.layout.middle; 
       break; 
      case 3: 
       resId = R.layout.right; 
       break; 
      case 4: 
       resId = R.layout.farright; 
       break; 
      } 

      View view = inflater.inflate(resId, null); 

      ((ViewPager) collection).addView(view, 0); 

      return view; 
     } 

     @Override 
     public void destroyItem(View arg0, int arg1, Object arg2) { 
      ((ViewPager) arg0).removeView((View) arg2); 

     } 

     @Override 
     public void finishUpdate(View arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public boolean isViewFromObject(View arg0, Object arg1) { 
      return arg0 == ((View) arg1); 

     } 

     @Override 
     public void restoreState(Parcelable arg0, ClassLoader arg1) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public Parcelable saveState() { 
      // TODO Auto-generated method stub 
      return null; 
     } 

     @Override 
     public void startUpdate(View arg0) { 
      // TODO Auto-generated method stub 

     } 


    } 

    @Override 
    public void onPageScrollStateChanged(int arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onPageScrolled(int arg0, float arg1, int arg2) { 
     // TODO Auto-generated method stub 

     updateUI(arg2); 
    } 

    @Override 
    public void onPageSelected(int arg0) { 
     // TODO Auto-generated method stub 
     //updateUI(arg0); 

    } 

    public void onNothingSelected(AdapterView<?> adapterView) {  
     final int i =0; 
     adapterView.postDelayed(new Runnable(){ 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       updateUI(i);    
      } 

     }, 5000); 
     Toast.makeText(this, "In nothing selected method",Toast.LENGTH_SHORT).show(); 




    } 

    private void updateUI(final int fi) { 
      /* handler.post(new Runnable() { 
       public void run() { 
        gallery.setSelection(i); 
        viewPager.setCurrentItem(i); 

        //textView.setText("Photo #" + i); 
       } 
      });*/ 
      handler.postDelayed(new Runnable() { 
       public void run() { 
        int i=fi; 
        int j = i; 
        myPager.setCurrentItem(j++); 
        System.out.println("Hello");    

       } 
      },1000); 
     } 


} 
+0

嘗試ViewSwitcher內。設計你的頁面並在幾秒鐘後調用next()的方法.. \ – 2012-04-06 11:48:08

+0

你可以給一個例子或代碼片段嗎? – 2012-04-16 17:45:59

+0

嘿好友,我已經實現了這個場景,如果你需要它只是讓我知道我會在這裏發佈解決方案 – krisDrOid 2012-07-19 09:32:26

回答

3

使用javautil定時器設置一個和定時器結束時,您可以使用

class UpdateTimeTask extends TimerTask { 
    public void run() { 
    //Code for the viewPager to change view 
    } 
} 


// Code to schedule the timer 
timer = new Timer(); 
timer.schedule(new UpdateTimeTask(), 100, 200); 

希望這有助於,使用android文檔的確切API調用

<ViewPager>.setCurrentitem(i++%<MaxViews>) 
+0

試過,onCreate(activity)和java.lang.IllegalStateException:必須從進程的主線程調用 :/ – GuilhE 2013-06-18 11:10:35

+0

查看「stackoverflow」上的這個答案:http://stackoverflow.com/a/17612369/1773155 – 2013-07-12 12:19:26

0

就UI更新而言,您可以通過處理程序來完成,所以調用timertask類中的處理程序,您可以在做這樣的事情:

Timer timer=null; 
static int time_tomove = 0; 

class UpdateTimeTask extends TimerTask { 
     public void run() { 
     //Code for the viewPager to change view 
      System.out.println("The value of the time_tomove "); 
      time_tomove++; 
      Message msg = handler_timer.obtainMessage(); 
      Bundle b = new Bundle(); 
      b.putInt("time", time_tomove); 
      msg.setData(b); 
      handler_timer.sendMessage(msg); 
     } 
    } 




public static Handler handler_timer = new Handler(){ 
    @Override 
    public void handleMessage(Message msg) { 
     super.handleMessage(msg); 
    magazineHolder.setCurrentItem(time_tomove); 
    } 
}; 

這樣做後,你可以調用下面兩行無論你的onResume或onCreate()方法

timer = new Timer(); 
     timer.schedule(new UpdateTimeTask(), 100, 2000); 
相關問題