onMeasure被调用太多次

时间:2018-12-05 09:57:45

标签: android layout measure onmeasure

我有一个布局为width == match_parent的布局(并且它的所有父级也是width == match_parent)。

在此布局中,有一个width == wrap_content

的textView
<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被调用了(上面的代码)。

这可能是什么原因?

顺便说一句,布局尺寸增加了,我不确定为什么。

0 个答案:

没有答案
相关问题