2013-06-04 9 views
17

L'intera documentazione per il metodo Fragment.onCreateAnimator(int, boolean, int) costituito dal testo seguente: "Chiamato quando un frammento carica un'animazione"Dov'è la documentazione di Fragment.onCreateAnimator()?

Questo è tutto. Nessuna spiegazione sui parametri.

Cosa significano i parametri? Even the source code doesn't reveal much.

+0

Sembra che questo risultato dell'utilizzo del metodo possa dare un'idea http://grepcode.com/search/usages?type=method&id=repository.grepcode.com%24java%[email protected]%[email protected] 2_r1 @ android% 24app @ Fragment @ onCreateAnimator% 28int% 2Coolean% 2Cint% 29 & k = u – sandrstar

risposta

6

Sulla base FragmentManager codice e usi di FragmentManagerImpl.loadAnimator(android.app.Fragment,int,boolean,int) sembra che Fragment.onCreateAnimator(int, boolean, int) permette di definire proprie animazioni per frammento di nascondersi, mostrando, cambiamento di stato. Tuttavia, non ne ho mai visto l'utilizzo in app reali.

parametri relativi:

  • int transit - tipo di transizione (costanti FragmentTransaction, ad esempio utilizzati in here);
  • boolean enter - true se è stato inserito, falso - altrimenti;
  • int transitionStyle - ID di stile da risorse (quello stile potrebbe contenere animazioni perse da onCreateAnimator);
+0

Grazie per approfondire ulteriormente. Ho messo un'istruzione 'Log.d()' all'inizio del metodo 'onCreateAnimator()' e ho scoperto che 'transit' è sempre impostato a' 0' quando si scambiano i frammenti. –

+0

@NathanOsman stai chiamando setTransit() sulla tua transazione frammento? – JakeCataford

13

Il metodo onCreateAnimator è dispari. Il prototipo che ho visto è questo:

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

int transit - tipo di transizione, come detto sopra sandrstar

boolean enter - vero se 'entrare,' false altrimenti

int nextAnim - Il ID risorsa dell'animazione che sta per giocare.

Così, per esempio, se si è tentato di fare un flip carta, 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(); 

NOTA: R.id.container_view nell'esempio di cui sopra è l'ID di un ViewGroup che contiene il frammento esistente stai provando a sostituire.

Quando viene eseguito il codice precedente, il metodo onCreateAnimator verrà chiamato, e il parametro nextAnim sarà uno dei quattro ID animazione passati alla funzione setCustomAnimations(), cioè R.animator.card_flip_right_in, R.animator.card_flip_right_out. .. ecc.

Questo non sembra immediatamente utile all'inizio, dal momento che non fornisce un riferimento all'oggetto Animator effettivo a cui è possibile collegare un listener.Ma stranamente, si può solo gonfiare un'altra Animator direttamente dalla risorsa nextAnim, e quindi collegare gli ascoltatori a questo, che, stranamente, sparare tutti i callback ignorati al momento giusto:

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

In questo modo, si dovrebbe essere in grado di aggiungere ascoltatori agli animatori di transizione dei frammenti come se li avessi creati tu stesso.

Problemi correlati