Android 在布局中删除/添加的视图未正确重新绘制

Android 在布局中删除/添加的视图未正确重新绘制,android,view,viewgroup,Android,View,Viewgroup,我试图在视图组的联机布局期间删除和添加视图,但这会导致某些子视图无法正确重画 考虑以下观点: public static class MyViewGroup extends ViewGroup { View view; int count; boolean removeInLayout = true; public MyViewGroup(Context context) { super(context); init(); } public My

我试图在视图组的联机布局期间删除和添加视图,但这会导致某些子视图无法正确重画

考虑以下观点:

public static class MyViewGroup extends ViewGroup {

  View view;

  int count;

  boolean removeInLayout = true;

  public MyViewGroup(Context context) {
    super(context);
    init();
  }

  public MyViewGroup(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  private void init() {
    view = LayoutInflater.from(getContext()).inflate(R.layout.myview, this, false);
    addView(view);
    setOnClickListener(new OnClickListener() {
      @Override public void onClick(View v) {
        count++;
        requestLayout();
      }
    });
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if (removeInLayout) {
      removeViewInLayout(view);
    }

    invalidate();

    final String text = "Count: " + count;
    ((TextView) view.findViewById(R.id.one)).setText(text);
    ((TextView) view.findViewById(R.id.two)).setText(text);
    ((TextView) view.findViewById(R.id.three)).setText(text);
    ((TextView) view.findViewById(R.id.four)).setText(text);

    if (removeInLayout) {
      LayoutParams lp = view.getLayoutParams();
      addViewInLayout(view, 0, lp);
      final int wms = MeasureSpec.makeMeasureSpec(r - l, MeasureSpec.EXACTLY);
      final int hms = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
      view.measure(wms, hms);
    }

    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
  }

  @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
        MeasureSpec.getSize(heightMeasureSpec));

    measureChild(view, widthMeasureSpec, heightMeasureSpec);
  }
}
将以下xml布局作为子项:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="84dp">

  <TextView
      android:id="@+id/one"
      android:layout_width="100dp"
      android:layout_height="84dp"
      android:background="#FF00FF00"
      tools:text="First View"/>

  <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@id/one"
      android:layout_toLeftOf="@+id/four"
      android:layout_centerVertical="true"
      android:layout_marginLeft="4dp"
      android:paddingBottom="2dp"
      android:orientation="vertical">

    <TextView
        android:id="@+id/two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        tools:text="Second View"/>

    <TextView
        android:id="@+id/three"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:fontFamily="sans-light"
        android:maxLines="3"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="12sp"
        tools:text="Third View"/>
  </LinearLayout>

  <TextView
      android:id="@+id/four"
      android:layout_width="100dp"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_centerVertical="true"
      android:layout_marginRight="4dp"
      android:layout_marginLeft="4dp"
      tools:text="Fourth view"/>
</RelativeLayout>

最初,视图及其子视图绘制得很好,在第一次单击时,每个视图都会重新绘制。任何进一步的点击只会导致id为“1”和“4”的视图被重新绘制。“二”和“三”被卡住了。 如果我从onLayout中删除
invalidate()
,视图将永远不会重新绘制

如果我将
removeViewInLayout
addViewInLayout
detachViewFromParent
attachViewToParent
交换掉,视图将正确地重新绘制。如果我删除了invalidate,它们将永远不会被重新绘制

有人能解释一下发生了什么事吗?是否在布局阶段无法删除和重新添加视图?为什么子视图在分离时无效的事实还不足以导致重新绘制

我已经上传了一个示例应用程序,点击屏幕上的任意位置来启动OnClickListener,它增加count变量并请求布局