title: ConstraintLayout GONE view occupies space
body: <p>I have a <code>ViewHolder</code> with the header on top and that header becomes visible in a specific case. In most other cases, the header is set as <code>GONE</code>. The problem is that when a header is set as GONE, its height is still calculated and other views are <code>spread</code> differently (with more space between). </p>
  
  <p>Here is a layout blueprint:
  <a href="https://i.stack.imgur.com/xh5lI.png" rel="noreferrer"><img src="https://i.stack.imgur.com/xh5lI.png" alt="blueprint"></a></p>
  
  <p>Blueprint explanation:</p>
  
  <ol>
  <li>The header is constrained to <code>top</code>, <code>left</code> and <code>right</code>.</li>
  <li>Two views below are in <code>packed chain</code>, constrained to a header on <code>top</code>, <code>ImageView</code> to the <code>right</code> and parent to the <code>left</code> and <code>bottom</code>.</li>
  </ol>
  
  <p>And here is a screenshot from the layout inspector with highlighted header view which is set as <code>GONE</code>:
  <a href="https://i.stack.imgur.com/f2fPK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/f2fPK.png" alt="enter image description here"></a></p>
  
  <p>According to documentation, the header view, when set to <code>GONE</code> should be shrunk to point with constraints from other views still applied to it, but the header should not occupy layout space and affect the height of the <code>ConstraintLayout</code> as it is set as <code>wrap_content</code>.</p>
  
  <p>In this inspector screenshot, it's not clear what happened actually. The header is not visible, a bottom view is obviously constrained to parent top, but the header is still shown in the inspector as full width with specified height. </p>
  
  <p>I'm not sure if this is a bug or I should force <code>ConstraintLayout</code> to re-measure itself.</p>
  
  <p><strong>XML UPDATE:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.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:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
  
      &lt;TextView
          android:id="@+id/list_item_step_conversion_tv_header"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:background="@color/gray_very_light"
          android:padding="@dimen/activity_vertical_margin"
          android:text="@string/favorites"
          android:textColor="@android:color/black"
          android:textSize="@dimen/default_text_size"
          android:textStyle="bold"
          android:visibility="gone"
          tools:visibility="visible"
          app:layout_constraintHorizontal_bias="0.0"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"/&gt;
  
      &lt;TextView
          android:id="@+id/list_item_step_conversion_tv_title"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginEnd="16dp"
          android:layout_marginLeft="16dp"
          android:layout_marginRight="16dp"
          android:layout_marginStart="16dp"
          android:layout_marginTop="8dp"
          android:ellipsize="end"
          android:maxLines="1"
          android:textColor="@color/gray_dark"
          android:textSize="@dimen/medium_text_size"
          app:layout_constraintBottom_toTopOf="@+id/list_item_step_conversion_tv_description"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toLeftOf="@+id/list_item_step_conversion_iv_favorite"
          app:layout_constraintTop_toBottomOf="@+id/list_item_step_conversion_tv_header"
          app:layout_constraintVertical_chainStyle="packed"
          tools:text="Bicycling - light (10-11.9 mph)"/&gt;
  
      &lt;TextView
          android:id="@+id/list_item_step_conversion_tv_description"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp"
          android:layout_marginLeft="0dp"
          android:layout_marginRight="16dp"
          android:layout_marginTop="4dp"
          android:ellipsize="end"
          android:maxLines="1"
          android:textColor="@android:color/black"
          android:textStyle="bold"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintHorizontal_bias="0.0"
          app:layout_constraintLeft_toLeftOf="@+id/list_item_step_conversion_tv_title"
          app:layout_constraintRight_toLeftOf="@+id/list_item_step_conversion_iv_favorite"
          app:layout_constraintTop_toBottomOf="@+id/list_item_step_conversion_tv_title"
          tools:text="182 steps/minute"/&gt;
  
      &lt;ImageView
          android:id="@+id/list_item_step_conversion_iv_favorite"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="0dp"
          android:layout_marginRight="24dp"
          android:layout_marginTop="0dp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/list_item_step_conversion_tv_header"
          app:srcCompat="@drawable/ic_not_liked"/&gt;    
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>After additional observations, this problem occurs only after a call to <code>notifyDataSetChanged</code> in <code>RecyclerView.Adapter</code>. Here is a screenshot of layout state before and after the click to the <code>favorite</code> icon to one of the items.
  <a href="https://i.stack.imgur.com/pxxsL.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pxxsL.png" alt="enter image description here"></a></p>
  
  <p>Screenshots explanation:</p>
  
  <ul>
  <li>On the left side, <code>ViewHolder</code> with visible <code>header</code> view is on <code>position: 2</code>. Items above are displayed correctly. </li>
  <li>After click on <code>favorite</code> icon (item with value 242), <code>ViewHolder</code> on <code>position: 1</code> is the one with visible <code>header</code> view, while the <code>ViewHolder</code> on <code>position: 2</code> have <code>header</code> view set as <code>GONE</code>. I was expecting for <code>ViewHolder</code> height to decrease and have the same height as <code>ViewHolder</code> on <code>position: 0</code>.</li>
  </ul>
  
  <p>Having in mind that this <code>ViewHolder</code> had <code>header</code> set to <code>VISIBLE</code> in previous state, it might have something with recycling, not sure.</p>