The ConstraintLayout GONE window takes up space

I have a ViewHolder with a title at the top, and that title becomes visible in a specific case. In most other cases, the title is set to GONE . The problem is that when the title is set to GONE, its height is still calculated, and other types of spread are different (with lots of spaces between them).

Here is the layout plan: blueprint

Short description:

  • The title is limited to top , left and right .
  • The two views below are in a packed chain , limited to a header on top , an ImageView on right and a parent for left and bottom .

And here is a screenshot from the layout inspector with a highlighted header that is set to GONE : enter image description here

According to the documentation, the presentation of the header, when set to GONE , should be shortened to indicate other types of restrictions that still apply to it, but the header should not occupy layout space and affect the height of the ConstraintLayout as it is set as wrap_content .

In this inspector screenshot, it is not clear what really happened. The title is not displayed, the bottom view is clearly limited by the parent top, but the title is still displayed in the inspector as the full width with the specified height.

I'm not sure if this is a mistake, or should I force ConstraintLayout re-measure myself.

XML UPDATE:

 <?xml version="1.0" encoding="utf-8"?> <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"> <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"/> <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)"/> <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"/> <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"/> </android.support.constraint.ConstraintLayout> 

UPDATE 2

After additional observations, this problem only occurs after calling notifyDataSetChanged in the RecyclerView.Adapter . Here is a screenshot of the layout state before and after clicking on the favorite icon on one of the elements. enter image description here

Screenshot explanation:

  • On the left side of the ViewHolder with a visible view, the header is at position: 2 . The items above are displayed correctly.
  • After clicking on the favorite icon (an element with a value of 242), ViewHolder on position: 1 is the view with a visible header , and ViewHolder on position: 2 has a header GONE . I expected the height of the ViewHolder decrease and will have the same height as the ViewHolder at position: 0 .

Bearing in mind that this ViewHolder was set to VISIBLE in the previous state, it may have something with reprocessing, not sure.

+5
source share
2 answers

I solved this problem by calling view.requestLayout() * at the end of my #getView adapter, after the usual call to binding.executePendingBindings() .

* Now I am using the old #onBindViewHolder() school, so you should look for the equivalent of RecyclerView.Adapter , perhaps #onBindViewHolder()

+1
source

It seems that the layout is actually correct - ignore the frame of the departed object in the inspector, when the widget is marked as departed, we do not upload it again, as it will simply be skipped (in Studio we do to provide an easier way to see what happens, but it makes no sense to do it on a real device).

What version of ConstraintLayout are you using? It seems to me that the correct behavior is here:

enter image description here

After marking the first element:

enter image description here

0
source

Source: https://habr.com/ru/post/1267280/


All Articles