2013-10-27 81 views

回答

25

我會建議做一些基類,你所有的Fragments擴展,並在其中定義一些可以覆蓋處理動畫事件的方法。然後,覆蓋onCreateAnimation()(假設您正在使用支持庫)在動畫回調中發送事件。例如:

protected void onAnimationStarted() {} 

protected void onAnimationEnded() {} 

protected void onAnimationRepeated() {} 

@Override 
public Animation onCreateAnimation (int transit, boolean enter, int nextAnim) { 
    //Check if the superclass already created the animation 
    Animation anim = super.onCreateAnimation(transit, enter, nextAnim); 

    //If not, and an animation is defined, load it now 
    if (anim == null && nextAnim != 0) { 
     anim = AnimationUtils.loadAnimation(getActivity(), nextAnim); 
    } 

    //If there is an animation for this fragment, add a listener. 
    if (anim != null) { 
     anim.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart (Animation animation) { 
       onAnimationStarted(); 
      } 

      @Override 
      public void onAnimationEnd (Animation animation) { 
       onAnimationEnded(); 
      } 

      @Override 
      public void onAnimationRepeat (Animation animation) { 
       onAnimationRepeated(); 
      } 
     }); 
    } 

    return anim; 
} 

然後,你Fragment子類,只是重寫onAnimationStarted()以禁用按鈕,並onAnimationEnded()啓用按鈕。

+2

這不適用於像Slide或Explode這樣的材質轉換,因爲anim始終爲空。 – Servus7