2013-10-27 5 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サブクラスのために、ただボタンを有効にするには、ボタン、およびonAnimationEnded()を無効にするonAnimationStarted()をオーバーライドします。

+2

'anim'は常にnullなので、SlideやExplodeのようなマテリアルトランジションでは機能しません。 – Servus7

関連する問題