2013-10-27 15 views

Trả lời

25

Điều tôi đề nghị là tạo một số lớp cơ sở mà tất cả các số Fragments của bạn mở rộng, và bên trong nó, xác định một vài phương pháp có thể được ghi đè để xử lý các sự kiện hoạt ảnh. Sau đó, ghi đè onCreateAnimation() (giả sử bạn đang sử dụng thư viện hỗ trợ) để gửi một sự kiện trên gọi lại hoạt ảnh. Ví dụ:

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

Sau đó, cho Fragment lớp con của bạn, chỉ cần ghi đè onAnimationStarted() để vô hiệu hóa các nút, và onAnimationEnded() để cho phép các nút.

+2

Điều này không hoạt động với các chuyển tiếp tài liệu như Trang trình bày hoặc Phát nổ vì 'hoạt ảnh' luôn là rỗng. – Servus7

Các vấn đề liên quan