2013-06-04 10 views
17

Toàn bộ tài liệu hướng dẫn cho phương pháp Fragment.onCreateAnimator(int, boolean, int) bao gồm các văn bản sau đây: "Được gọi khi một đoạn tải một hình ảnh động"Tài liệu cho Fragment.onCreateAnimator() ở đâu?

Vậy đó. Không giải thích về các tham số.

Các thông số có ý nghĩa gì? Even the source code doesn't reveal much.

+0

Có vẻ như kết quả này của việc sử dụng phương pháp có thể đưa ra một ý tưởng http://grepcode.com/search/usages?type=method&id=repository.grepcode.com%24java%[email protected]%[email protected] 2_r1 @ android% 24app @ Phân đoạn @ onCreateAnimator% 28int% 2Cboolean% 2Cint% 29 & k = u – sandrstar

Trả lời

6

Dựa trên FragmentManager mã và sử dụng số FragmentManagerImpl.loadAnimator(android.app.Fragment,int,boolean,int) có vẻ như là Fragment.onCreateAnimator(int, boolean, int) cho phép bạn xác định hoạt ảnh riêng để ẩn, ẩn, hiển thị trạng thái. Tuy nhiên, tôi chưa bao giờ thấy việc sử dụng nó trong các ứng dụng thực sự.

thông số Về:

  • int transit - loại chuyển tiếp (hằng FragmentTransaction, ví dụ sử dụng trong here);
  • boolean enter - true nếu trạng thái của nó nhập, sai - nếu không;
  • int transitionStyle - id kiểu từ tài nguyên (kiểu đó có thể chứa hoạt ảnh bị bỏ lỡ từ onCreateAnimator);
+0

Cảm ơn bạn đã tìm hiểu thêm về điều này. Tôi đặt câu lệnh 'Log.d()' vào đầu phương thức 'onCreateAnimator()' và phát hiện rằng 'transit' luôn được đặt thành' 0' khi hoán đổi các đoạn. –

+0

@NathanOsman có phải bạn đang gọi setTransit() trên giao dịch phân đoạn không? – JakeCataford

13

Phương pháp onCreateAnimator là lẻ. Nguyên mẫu tôi đã nhìn thấy là thế này:

public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)

int transit - loại chuyển tiếp, như đã nói ở trên sandrstar

boolean enter - đúng nếu 'vào,' false

int nextAnim - Các ID tài nguyên của hoạt ảnh sắp phát.

Vì vậy, ví dụ, nếu bạn đã thử làm một lật thẻ, from the documentation:

// Create and commit a new fragment transaction that adds the fragment for the back of 
// the card, uses custom animations, and is part of the fragment manager's back stack. 
BackOfCardFragment backFragment = new BackOfCardFragment(); 

getFragmentManager() 
    .beginTransaction() 

    // Replace the default fragment animations with animator resources representing 
    // rotations when switching to the back of the card, as well as animator 
    // resources representing rotations when flipping back to the front (e.g. when 
    // the system Back button is pressed). 
    .setCustomAnimations(
     R.animator.card_flip_right_in, R.animator.card_flip_right_out, 
     R.animator.card_flip_left_in, R.animator.card_flip_left_out) 

    // Replace any fragments currently in the container view with a fragment 
    // representing the next page (indicated by the just-incremented currentPage 
    // variable). 
    .replace(R.id.container_view, backFragment) 

    // Add this transaction to the back stack, allowing users to press Back 
    // to get to the front of the card. 
    .addToBackStack(null) 

    // Commit the transaction. 
    .commit(); 

LƯU Ý: R.id.container_view trong ví dụ trên là ID của một ViewGroup có chứa đoạn hiện bạn đang cố gắng thay thế.

Khi đoạn mã trên được thực thi, phương pháp onCreateAnimator sẽ được gọi, và tham số nextAnim sẽ là một trong bốn ID hình ảnh động chuyển vào setCustomAnimations() chức năng, tức là R.animator.card_flip_right_in, R.animator.card_flip_right_out. .. vv

Điều này dường như không hữu ích ngay lập tức lúc đầu, vì nó không cung cấp cho bạn tham chiếu đến đối tượng Animator thực tế mà bạn có thể đính kèm người nghe vào.Nực cười là, bạn chỉ có thể thổi phồng Animator khác trực tiếp từ nguồn nextAnim, và sau đó đính kèm các thính giả đó, nhờ đó sẽ kỳ quặc, bắn tất cả các callbacks ghi đè vào đúng thời điểm:

@Override 
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { 
    Animator animator = null; 
    // In this example, i want to add a listener when the card_flip_right_in animation 
    // is about to happen. 
    if (nextAnim == R.animator.card_flip_right_in) { 
     animator = AnimatorInflater.loadAnimator(getActivity(), nextAnim); 
     // * Sometimes onCreateAnimator will be called and nextAnim will be 0, 
     // causing animator to be null. 
     // * I wanted to add a listener when the fragment was entering - 
     // your use case may be different. 
     if (animator != null && enter) { 

      animator.addListener(new Animator.AnimatorListener() { 
       @Override 
       public void onAnimationStart(Animator animation) { 
        // Do something when the card flip animation begins 
       } 

       @Override 
       public void onAnimationEnd(Animator animation) { 
        // Do something as soon as the card flip animation is over 
       } 

       @Override 
       public void onAnimationCancel(Animator animation) { 
       } 

       @Override 
       public void onAnimationRepeat(Animator animation) { 
       } 
      }); 
     } 
    } 
    return animator; 
} 

Bằng cách này, bạn nên có thể thêm người nghe vào hoạt ảnh chuyển tiếp phân đoạn như thể bạn đã tự tạo chúng.