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:
- Cómo mostrar el fragmento de entrada sobre el fragmento de salida mientras se anima.
- ¿Qué hace exactamente FragmentManager y FragmentTransaction exactamente?
- android.R.id.content como contenedor para Fragment
- Cómo reemplazar un ViewPager con un fragmento
- Fragmento de Android La transacción con animación provoca que el flash blanco
<?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í.
- Android TextInputLayouts perder texto / contenido al volver en la transacción de fragmentos
- Android: ¿Cuándo es apropiado utilizar FragmentTransaction.remove?
- ViewPager dentro de Fragmento desaparece después de transaction.remove () (Mantener instancia?)
- ¿Cómo funciona el nuevo FragmentTransaction commitNow () trabajando internamente?
- ¿Cómo reemplazar el fragmento C con el fragmento A cuando se pulsa el botón Atrás?
- Fragmento de Android: ¿se mueve de una vista a otra?
- Android: Fragmento de diálogo y problemas de Backstack
- Java.lang.IllegalArgumentException: No se encontró ninguna vista para id 0x1020002 (android: id / content) para el fragmento
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.
- Problema con el zoom de texto de WebView en Android
- ¿Cómo puedo rastrear las ubicaciones en interiores?