2013-06-04 17 views
17

Cała dokumentacja dla metody Fragment.onCreateAnimator(int, boolean, int) składa się z poniższym tekstem: „Called gdy fragment ładuje animację”Gdzie jest dokumentacja dla Fragment.onCreateAnimator()?

To wszystko. Brak wyjaśnień dotyczących parametrów.

Co oznaczają parametry? Even the source code doesn't reveal much.

+0

Wydaje się, że wyniki użycia metody mogą dać pojęcie http://grepcode.com/search/usages?type=method&id=repository.grepcode.com%24java%[email protected]%[email protected] 2_r1 @ android% 24app @ Fragment @ onCreateAnimator% 28int% 2Cboolean% 2Cint% 29 & k = u – sandrstar

Odpowiedz

6

podstawie kodu FragmentManager i zwyczajów FragmentManagerImpl.loadAnimator(android.app.Fragment,int,boolean,int) wydaje się, że Fragment.onCreateAnimator(int, boolean, int) pozwala zdefiniować własne animacje dla fragmentu ukrycia, pokazując, zmieniając stan. Jednak nigdy nie widziałem jego użycia w prawdziwych aplikacjach.

Odnośnie parametrów:

  • int transit - typ przejścia (stałe FragmentTransaction, na przykład stosowanych w here);
  • boolean enter - true jeśli jest to stan wprowadź, fałsz - w przeciwnym razie;
  • int transitionStyle - identyfikator stylu z zasobów (ten styl może zawierać animacje pominięte w onCreateAnimator);
+0

Dzięki za wkopanie się w to dalej. Wstawiłem instrukcję 'Log.d()' na początku metody 'onCreateAnimator()' i odkryłem, że 'transit' jest zawsze ustawione na' 0' podczas zamiany fragmentów. –

+0

@NathanOsman wywołujesz setTransit() w transakcji twojego fragmentu? – JakeCataford

13

Metoda onCreateAnimator jest nieparzysta. Prototyp Widziałem to:

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

int transit - rodzaj przejścia, jak sandrstar powiedziane wyżej

boolean enter - prawda czy 'wejściu', w przeciwnym wypadku false

int nextAnim - identyfikator zasobu animacji, która ma zostać odtworzona.

Tak więc, na przykład, jeśli spróbujesz zrobić flipa karty, 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(); 

UWAGA: R.id.container_view w powyższym przykładzie jest identyfikator z ViewGroup że zawiera istniejący fragment próbujesz zastąpić.

Kiedy powyższy kod jest wykonywany, metoda onCreateAnimator zostanie sprawdzony, a parametr nextAnim będzie jednym z czterech identyfikatorów animacji przekazywanych do funkcji setCustomAnimations(), tj R.animator.card_flip_right_in, R.animator.card_flip_right_out. .. itd.

Na pierwszy rzut oka wydaje się to nieprzydatne, ponieważ nie zawiera odniesienia do rzeczywistego obiektu animatora, do którego można dołączyć słuchacza.Ale dziwnie, wystarczy nadmuchać inny Animator bezpośrednio z zasobu nextAnim, a następnie dołączyć słuchaczy tym, który będzie dziwnie, ogień wszystkie przesłonięte zwrotnych w odpowiednim czasie:

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

W ten sposób, należy móc dodawać słuchaczy do animatorów przejścia fragmentów tak, jak gdybyś sam je stworzył.

Powiązane problemy