Значение getY () не изменяется после изменения высоты просмотра - PullRequest
0 голосов
/ 19 апреля 2019

У меня есть структура кадра, к которой я добавляю различные виды высоты в зависимости от различных обстоятельств.

Я хотел бы знать положение оси Y макета кадра до и после добавления вида.

Поэтому, прежде чем добавить представление, я вызываю getY() в макете кадра, который возвращает 1891, что правильно, после добавления нового представления он по-прежнему возвращает 1891, что неверно, должно быть меньше, так как макет кадра выше вверх по экрану.

Я проверил инспектор макета для обоих обстоятельств, и getY() дает правильное значение, так почему мой код не работает?

Я пытался вызвать rootLayout.requestLayout(), чтобы перерисовать макет после добавления представления, но это не сработало.

    Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));

    LayoutInflater.from(getContext()).inflate(R.layout.main_menu_3_item, frameLayout, true);

    questionRoot.requestLayout();

    Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));

Вот мой XML

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/questionRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/colorBackground"
        android:clickable="true"
        android:focusable="true"
        tools:context=".QuestionFragments.Question">

        <com.englishquestionstogo.CustomViews.CustomChronometer
            android:id="@+id/timer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
             />

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/actionFrameLayout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <RelativeLayout
                android:id="@+id/relativeLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/questionTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="32dp"
                    android:layout_marginEnd="16dp"
                    android:textAlignment="center"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    tools:text="Title" />

                <TextView
                    android:id="@+id/questionBody"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/questionTitle"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="24dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="8dp"
                    android:paddingBottom="32dp"
                    android:textSize="16sp" />

            </RelativeLayout>

        </ScrollView>

        <View
            android:id="@+id/topBorder"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#D3D3D3"
            app:layout_constraintBottom_toTopOf="@id/frameLayout"
            />

        <View
            android:id="@+id/bottomBorder"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#D3D3D3"
            app:layout_constraintBottom_toBottomOf="parent"
            />


        <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@color/white"
            app:layout_constraintBottom_toTopOf="@id/bottomBorder"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <Button
                android:id="@+id/testButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />


        </FrameLayout>


        <FrameLayout
            android:id="@+id/actionFrameLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toTopOf="@id/frameLayout"
            >


        </FrameLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

1 Ответ

1 голос
/ 19 апреля 2019

Вы должны использовать addOnGlobalLayoutListener

 actionFrameLayout.getViewTreeObserver().addOnGlobalLayoutListener(
   new ViewTreeObserver.OnGlobalLayoutListener() {
      public void onGlobalLayout() {
           //Remove the listener before proceeding
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                actionFrameLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
           } else {
                actionFrameLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
           }

           // get the absolute coordinates of a view
           Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));

      }
   }
 );
...