GetLocationOnScreen devuelve la posición antigua después de la rotación

Lo que tengo:

  • Una actividad con android:configChanges="orientation|screenSize|keyboardHidden"
  • Un DialogFragment en él
  • ViewA en DialogFragment

Cuál es el problema:

Estoy usando ViewA.getLocationOnScreen para obtener la ubicación en la pantalla de la vista. Cuando abrí el diálogo por primera vez la posición es correcta. Después de girar la pantalla, debido a android:configChanges la vista de alguna manera no actualiza su posición e incluso si el diálogo se centra correctamente en la actividad de la getLocationOnScreen de ViewA puntos a la antigua ubicación, antes de la rotación.

Lo que he intentado.

onConfigurationChanged el onConfigurationChanged del diálogo y probé esto:

  • ViewA.requestLayout (no hace nada)
  • ViewA.getViewTreeObserver().addOnGlobalLayoutListener y onGlobalLayout establecen el topMargin en 1 y llaman requestLayout nuevamente. (Esto funcionó, pero no quiero establecer el margen cada vez que giro la pantalla)

Lo que quiero saber es cómo puedo forzar la reposición del diálogo, haciendo getLocationOnScreen devolver los valores correctos después de una rotación

android:configChanges cuenta que no quiero cambiar android:configChanges

Para tratar de explicar, si tienes este android:configChanges="orientation no se realizará recreación de Activity , lo que significa que no se medirán las View , por eso se informan las posiciones anteriores, también para solicitar un sorteo completo en tu View que necesita o llámalo en el DialogFrament la vista de DialogFrament para que el juego general su DecorView – call invalidate() & requestLayout()

La ubicación de la vista en la pantalla todavía no se ha actualizado cuando onConfigurationChanged se llama. OnLayoutChangeListener agregar un OnLayoutChangeListener a la vista para capturar las actualizaciones que está buscando. Vea el siguiente ejemplo.

TestDialogFragment.java

 public class TestDialogFragment extends DialogFragment { private static final String TAG = "TestDialogFragment"; View testView; int[] testViewLocation = {0, 0}; public TestDialogFragment() {} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.test_fragment, container); testView = view.findViewById(R.id.test_view); testView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { Log.d(TAG, "onLayoutChange"); testView.getLocationOnScreen(testViewLocation); Log.d(TAG, String.format("%s %s", testViewLocation[0], testViewLocation[1])); } }); return view; } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.d(TAG, "onConfigurationChanged"); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Log.d(TAG, "landscape"); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ Log.d(TAG, "portrait"); } testView.getLocationOnScreen(testViewLocation); Log.d(TAG, String.format("%s %s", testViewLocation[0], testViewLocation[1])); } } 

Test_fragment.xml

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/test_view" android:layout_height="50dp" android:layout_width="match_parent" android:layout_gravity="center"/> </FrameLayout> 

Salida de registro

 06-24 16:20:05.682 D/TestDialogFragment﹕ onConfigurationChanged 06-24 16:20:05.682 D/TestDialogFragment﹕ portrait 06-24 16:20:05.682 D/TestDialogFragment﹕ 504 601 06-24 16:20:05.852 D/TestDialogFragment﹕ onLayoutChange 06-24 16:20:05.852 D/TestDialogFragment﹕ 84 1021 06-24 16:20:08.695 D/TestDialogFragment﹕ onConfigurationChanged 06-24 16:20:08.695 D/TestDialogFragment﹕ landscape 06-24 16:20:08.695 D/TestDialogFragment﹕ 84 1021 06-24 16:20:08.865 D/TestDialogFragment﹕ onLayoutChange 06-24 16:20:08.865 D/TestDialogFragment﹕ 504 601 06-24 16:20:13.550 D/TestDialogFragment﹕ onConfigurationChanged 06-24 16:20:13.550 D/TestDialogFragment﹕ portrait 06-24 16:20:13.550 D/TestDialogFragment﹕ 504 601 
  • DailogFragment - getArguments / setArguments - ¿por qué pasar los argumentos en un lote?
  • ¿Cómo configurar el ancho y la altura de DialogFragment?
  • IllegalStateException cuando se utiliza DialogFragment
  • DialogFragment con fondo claro (no atenuado)
  • Prueba de DialogFragments con Robolectric
  • ¿Es posible iniciar un DialogFragment desde una preferencia en una preferenciaActivity?
  • Android Alert Dialog no puede encontrar la vista
  • Android DialogFragment está en blanco
  • DialogFragment anidado se descarta después de la rotación
  • DialogFragment no cambia el tamaño cuando se muestra el teclado
  • Pantalla de relleno DialogFragment en el teléfono
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.