2013-10-27 3 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

'anim'이 항상 null이기 때문에 슬라이드 또는 폭발과 같은 머티리얼 전환에서는 작동하지 않습니다. – Servus7