Cambiar el orden z de Fragmentos durante la FragmentTransaction en curso

¿Hay alguna manera de cambiar el orden z en el que se muestran fragmentos durante una FragmentTransaction en curso? Tengo una animación donde ambos fragmentos se superponen entre sí y me gustaría tener el fragmento que se desliza desde la derecha (el segundo fragmento) se muestra debajo de la otra que se desliza hacia la izquierda. En este momento se muestran en orden inverso durante la transacción.

Aquí está el código de una de mis animaciones:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="400" android:zAdjustment="bottom"> <scale android:toXScale="1" android:fromXScale="0.9" android:pivotX="50%p" android:pivotY="50%p" android:toYScale="1" android:startOffset="300" android:fromYScale="0.9"/> <translate android:fromXDelta="50%p" android:interpolator="@android:interpolator/overshoot" android:toXDelta="0"/> </set> 

Y aquí está el código de la transacción

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.show(fragmentToShow).hide(fragmentToHide).commit(); 

Me gustaría tener el fragmentToHide para aparecer en el fragmentToShow . He tratado de abordar con el android:zAdjustment propiedad android:zAdjustment , pero ya que aparentemente sólo funciona para las animaciones de ventana que sólo no han funcionado para mí.

También tuve este problema y la única solución que he podido encontrar es colocar otro FrameLayout directamente debajo del que contiene el fragmento actual. Luego transito mi nuevo fragmento en el FrameLayout alternativo (que tiene un orden z más alto que FrameLayout debajo) y elimino el fragmento antiguo del FrameLayout original en la misma transacción. Algo como esto:

 <FrameLayout android:id="@+id/video_list_frame" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> <FrameLayout android:id="@+id/video_player_frame" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> 

Sí, sé que esto agrega una vista adicional a la jerarquía pero no aumenta la profundidad del árbol. Para un aumento de rendimiento adicional, anulo onCreateAnimator en mis fragmentos para descargar la animación a la capa de hardware, así:

 @Override public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { Animator animation = super.onCreateAnimator(transit, enter, nextAnim); // HW layer support only exists on API 11+ if (Build.VERSION.SDK_INT >= 11) { if (animation == null && nextAnim != 0) { animation = AnimatorInflater.loadAnimator(getActivity(), nextAnim); } if (animation != null) { animation.addListener(new LayerEnablingAnimatorListener(getView())); } } return animation; } 

Y esto es LayerEnablingAnimatorListener:

 public class LayerEnablingAnimatorListener extends AnimatorListenerAdapter { private final View mTargetView; private int mLayerType; public LayerEnablingAnimatorListener(View targetView) { mTargetView = Objects.requireNonNull(targetView, "Target view cannot be null"); } public View getTargetView() { return mTargetView; } @Override public void onAnimationStart(Animator animation) { super.onAnimationStart(animation); mLayerType = mTargetView.getLayerType(); mTargetView.setLayerType(View.LAYER_TYPE_HARDWARE, null); } @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); mTargetView.setLayerType(mLayerType, null); } } 

Utilice ViewCompat.setTranslationZ() y lo siguiente en su fragmento de red

 @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ViewCompat.setTranslationZ(getView(), 1f); } 

El trasnlationZ predeterminado es 0.

  • FragmentTransaction.replace reemplaza sólo el primer Fragment en un contenedor
  • FragmentTransacation con diapositiva en / out animación en> 4 rendimiento
  • Android - Hacer traducciones y objectAnimator en el mismo archivo XML
  • Cómo agregar / quitar Fragmento en el botón haga clic?
  • Fragmento Transacción cargar la vista vacía, pero el fragmento se muestra después de que el dispositivo giratorio
  • No se encontró ninguna vista para id - DialogFragment + Fragment
  • Cómo agregar un fragmento a un diseño de un DialogFragment?
  • Ciclo de vida del fragmento - ¿Qué método se llama mostrar / ocultar?
  • El artículo de DrawerLayout hace clic - ¿Cuándo es el momento adecuado para reemplazar el fragmento?
  • Salir de la animación no funciona; FragmentTransaction animación personalizada no funciona para ocultar
  • Mover fragmento de Android a un contenedor diferente No se puede cambiar el identificador de contenedor de fragmento
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.