onMeasure вызывается слишком много раз - PullRequest
0 голосов
/ 05 декабря 2018

У меня есть макет с макетом width == match_parent (и все его родители также width == match_parent).

В этом макете есть textView с width == wrap_content

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
  <android.support.design.chip.Chip
      android:id="@+id/my_account"   
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/og_my_account_desc_long_length"/>
  <View
      android:id="@+id/divider"
      style="@style/DividerStyle"/>
  <android.support.v7.widget.RecyclerView
      android:id="@+id/accounts_list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      tools:listitem="@layout/account_list_item"/>
</LinearLayout>

У меня есть это в этом макете Java-код:

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (myAccountView.getVisibility() == GONE) {
      return;
    }
    MarginLayoutParams layoutParams = (MarginLayoutParams) myAccountView.getLayoutParams();
    int currentTextMaxWidth =
        MeasureSpec.getSize(widthMeasureSpec)
            - (getPaddingLeft()
                + getPaddingRight()
                + myAccountView.getPaddingLeft()
                + myAccountView.getPaddingRight()
                + layoutParams.leftMargin
                + layoutParams.rightMargin);
    // Optimization: skip changing text and re-measuring when available space hasn't changed
    // from last call to this method.
    if (currentTextMaxWidth == lastTextMaxWidth) {
      return;
    }
    lastTextMaxWidth = currentTextMaxWidth;
    myAccountView.setText(getLongestPossibleChipText(currentTextMaxWidth));
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }

и каждый раз, когда я вызываю super.onMeasure (), я вижу другой вызов onMeasure ()

, который имеет смысл, как родитель имеет height==wrap_content

Однако в последнее время моя оптимизация блокирует еще один вызов super.onMeasure(), но я все еще вижу, что мой макет onMeasure вызывается (код выше).

в чем может быть причинадля этого?

кстати, размер макета увеличивается, и я не знаю почему.

...