2015-06-12 25 views
1

我想在佈局inflater的每個內容上顯示動畫。下面是我正在使用的代碼,但問題是動畫只顯示在第一頁的視圖尋呼機上。在每個viewpager更改上顯示動畫

到目前爲止,我所理解的是,所有頁面內容的動畫都被加載到位置0上,但我可能是錯的。

@Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View v = null; 
      if (position == 0) { 

       Home.rel_footer.setVisibility(View.VISIBLE); 

       v = inflater.inflate(R.layout.reward_points_circles, container, 
         false); 

       YoYo.with(Techniques.ZoomIn).duration(5000) 
         .playOn(v.findViewById(R.id.circle_1)); 

       YoYo.with(Techniques.ZoomIn).duration(5000) 
         .playOn(v.findViewById(R.id.circle_2)); 

       YoYo.with(Techniques.ZoomIn).duration(5000) 
         .playOn(v.findViewById(R.id.circle_3)); 

      } else if (position == 1) { 

       Home.rel_footer.setVisibility(View.GONE); 

       v = inflater.inflate(R.layout.qrcode_scanner_promotion, container, 
         false); 

       YoYo.with(Techniques.Landing).duration(5000) 
         .playOn(v.findViewById(R.id.imgView_iphone)); 

       YoYo.with(Techniques.Landing).duration(5000) 
         .playOn(v.findViewById(R.id.imgView_ipad)); 

      } else if (position == 2) { 

       Home.rel_footer.setVisibility(View.GONE); 

       v = inflater.inflate(R.layout.deals_promotion, container, false); 

       YoYo.with(Techniques.ZoomIn).duration(5000) 
         .playOn(v.findViewById(R.id.imgView_iphone_left)); 

       YoYo.with(Techniques.ZoomIn).duration(5000) 
         .playOn(v.findViewById(R.id.imageView_iphone_front)); 

       YoYo.with(Techniques.ZoomIn).duration(5000) 
         .playOn(v.findViewById(R.id.imgView_iphone_right)); 

      } 

      return v; 
     } 

回答

1

在,如果你想看到在每一頁上改變你的動畫,你不應該在onCreateView應用動畫我的意見。您應改爲使用OnPageChangeListener界面,並在用戶更換尋呼機後切換到另一頁面。

一個簡單的代碼片段,讓你開始。

mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){ 

    @Override 
    public void onPageSelected (int position){ 
     // Apply animations here w.r.t the position 
    } 

    ... Other overridden methods ... 
}); 
1
mViewPager.setOnPageChangeListener(new OnPageChangeListener() { 

     @Override 
     public void onPageSelected(int arg0) { 
      // TODO Auto-generated method stub 
      switch (arg0) { 
      case 1: 
       //load ur animations here 
       break; 

      default: 
       break; 
      } 
     } 

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

     } 

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

     } 
    });