2013-10-27 12 views

risposta

25

Ciò che suggerirei è di creare una classe base che estenda tutto il tuo Fragments e al suo interno definisca alcuni metodi che possono essere sovrascritti per gestire gli eventi di animazione. Quindi, eseguire l'override di onCreateAnimation() (supponendo che si stia utilizzando la libreria di supporto) per inviare un evento sui callback di animazione. Per esempio:

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; 
} 

Poi, per il vostro Fragment sottoclasse, basta sovrascrivere onAnimationStarted() per disabilitare i pulsanti, e onAnimationEnded() per attivare i tasti.

+2

Questo non funziona con le transizioni di materiale come Slide o Explode perché 'anim' è sempre nullo. – Servus7

Problemi correlati