2012-06-04 9 views
94

me pareció que el sistema podría revertir animaciones en la backstack cuando se pulsa el botón de retroceso cuando se utilizan fragmentos usando el siguiente código:Cómo revertir animaciones de fragmentos en BackStack?

FragmentManager fm = getFragmentManager(); 
FragmentTransaction ft = fm.beginTransaction(); 
ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out); 
ft.replace(R.id.viewContainer, new class(), "layout").addToBackStack(null).commit(); 

Respuesta

222

De acuerdo con la android documentation for custom animation:

Cambio:

ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out); 

Para:

ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out); 

y ahora el backstack anima - ¡En reversa!

+2

por cierto, sé que esto no está conectado a su pregunta y respuesta, pero ¿Podría tal vez me enlace a algo que explica un customAnimations ¿un poco? : P – AreusAstarte

+4

Muchas gracias. Gran respuesta. –

+2

AreusAstarte: ver http://developer.android.com/reference/android/app/FragmentTransaction.html#setCustomAnimations(int, int, int, int) – mDroidd

14

uso de la animación correcta He utilizado el siguiente y su trabajo como un encanto

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="@android:integer/config_mediumAnimTime" > 
    <objectAnimator 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:duration="500" 
     android:propertyName="x" 
     android:valueFrom="1000" 
     android:valueTo="0" 
     android:valueType="floatType" /> 
</set> 

slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="@android:integer/config_mediumAnimTime" > 

    <objectAnimator 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:duration="500" 
     android:propertyName="x" 
     android:valueFrom="0" 
     android:valueTo="1000" 
     android:valueType="floatType" /> 

</set> 

slide_out_left .xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="@android:integer/config_mediumAnimTime" > 

    <objectAnimator 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:duration="500" 
     android:propertyName="x" 
     android:valueFrom="0" 
     android:valueTo="-1000" 
     android:valueType="floatType" /> 

</set> 

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="@android:integer/config_mediumAnimTime" > 

    <objectAnimator 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:duration="500" 
     android:propertyName="x" 
     android:valueFrom="-1000" 
     android:valueTo="0" 
     android:valueType="floatType" /> 

</set> 

a continuación, utilizar siguiente al tiempo que añade fragmento

setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, 
           R.anim.slide_out_right, R.anim.slide_in_right) 

y será trabajado 100%

+2

esto no funcionará si está utilizando el gestor de fragmento de apoyo o si su fragmento se extiende la versión de soporte de Fragmento – w3bshark

+0

@ w3bshark Cómo obtener este tipo de animaciones de trabajo usando '' FragmentManager' y Fragment' de la biblioteca de apoyo? –

+1

@DanielShatz Debe usar las traducciones en lugar de objectAnimators. Por ejemplo, slide_in_left.xml sería: '' Ver esta respuesta: http://stackoverflow.com/a/5151774/1738090 – w3bshark

2
.setCustomAnimations(R.animator.fragment_fade_in, 
     R.animator.fragment_fade_out, 
     R.animator.fragment_fade_p_in, 
     R.animator.fragment_fade_p_out) 

remover lo anterior con:

mFragmentManager.beginTransaction() 
    .setCustomAnimations(R.animator.fragment_fade_in, 
      R.animator.fragment_fade_out, 
      R.animator.fragment_fade_p_in, 
      R.animator.fragment_fade_p_out) 
    .replace(R.id.main_container, FragmentPlayerInfo.getInstance(data)) 
    .addToBackStack(FragmentPlayerInfo.TAG) 
    .commit(); 
+1

Le recomendaría que agregue una explicación sobre cómo su recomendación ayuda. – Wtower

+2

No sé por qué funciona (:, pero cuando se añade la animación después de '' replace' y addToBackstack', no se trabaja – TarikW

+2

@TarikW soy poco tarde, pero el orden es importante en esto, es necesario llamar antes de reemplazar setCostomAnimations , métodos AddToBackStack – Tahir

1

en mi caso

ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, 
         R.anim.slide_in_right, R.anim.slide_out_left); 

sería crear la animación perfecta.

slide_in_right

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="50%p" android:toXDelta="0" 
       android:duration="@android:integer/config_mediumAnimTime"/> 
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
      android:duration="@android:integer/config_mediumAnimTime" /> 
</set> 

slide_out_left

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="0" android:toXDelta="-50%p" 
       android:duration="@android:integer/config_mediumAnimTime"/> 
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" 
      android:duration="@android:integer/config_mediumAnimTime" /> 
</set> 
+0

Cool ... Funcionó para mí. –

Cuestiones relacionadas