为什么在扩展视图后使用requestLayout()甚至在ConstrainLayout中相互重叠textViews?

时间:2017-08-22 09:05:22

标签: android android-constraintlayout

当用户点击项目底部变得可见时,底部是不可见的 视图未展开时的描述行(第3行)仅显示1行, 随着它变得扩大它应该显示尽可能多的3-4行, 但是当描述占据3-4行时,如果底部重叠,则为正文 我调用requestLayout但没有改变enter image description here

    if (holder.binding instanceof StoreOfferItemBinding) {
                final boolean isExpanded = position == expandedPosition;
                ((ItemBinding) (holder.binding)).offerDetail.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
                if (isExpanded) {
                    holder.itemView.requestLayout();
                }
                ((ItemBinding) (holder.binding)).offerDescription.setMaxLines(isExpanded ? 4 : 1);
                holder.itemView.setActivated(isExpanded);
                holder.itemView.setOnClickListener(view -> {
                    expandedPosition = isExpanded ? -1 : holder.getAdapterPosition();
                    notifyItemChanged(holder.getAdapterPosition());
                });
                if(isExpanded) {
                    holder.itemView.requestLayout();
                }
            }

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:background="?attr/selectableItemBackground">
  <TextView
        android:id="@+id/offer_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@{offer.productName()}"
        app:layout_constraintEnd_toStartOf="@+id/offer_add_to_user"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toRightOf="@+id/offer_image"
        app:layout_constraintRight_toLeftOf="@+id/offer_add_to_user"
        app:layout_constraintStart_toEndOf="@+id/offer_image"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Juice Valfria" />

    <TextView
        android:id="@+id/offer_price"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@{offer.offerCondition()}"
        app:layout_constraintEnd_toStartOf="@+id/offer_add_to_user"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintRight_toLeftOf="@+id/offer_add_to_user"
        app:layout_constraintStart_toEndOf="@+id/offer_image"
        app:layout_constraintTop_toBottomOf="@+id/offer_title"
        tools:text="34,50 kr/kg" />

    <TextView
        android:id="@+id/offer_description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ellipsize="end"
        android:lines="1"
        android:text="@{offer.sizeOrQuantity()}"
        app:layout_constraintEnd_toStartOf="@+id/offer_add_to_user"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toRightOf="@+id/offer_image"
        app:layout_constraintRight_toLeftOf="@+id/offer_add_to_user"
        app:layout_constraintTop_toBottomOf="@+id/offer_price"
        tools:text="Description" />

    <ImageView
        android:id="@+id/offer_image"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        app:imageUri="@{offer.imageUrl()}"
        app:layout_constraintBottom_toTopOf="@+id/offer_detail"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/offer_add_to_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:button="@drawable/checkbox_background"
        app:layout_constraintBottom_toTopOf="@+id/offer_detail"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/offer_detail"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/offer_description">

        <TextView
            android:id="@+id/price_comparison"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:text="@{offer.priceComparison()}"
            app:layout_constraintBottom_toTopOf="@+id/disclaimer"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Price Comparison" />


        <TextView
            android:id="@+id/disclaimer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:text="@{offer.disclaimer()}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/price_comparison"
            tools:text="Disclaimer" />

    </android.support.constraint.ConstraintLayout>


</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

   <android.support.constraint.ConstraintLayout
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:background="?attr/selectableItemBackground">

    <TextView
        android:id="@+id/offer_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/offer_price"
        app:layout_constraintEnd_toStartOf="@+id/offer_add_to_user"
        app:layout_constraintStart_toEndOf="@+id/offer_image"
        app:layout_constraintTop_toTopOf="parent"
    />

    <TextView
        android:id="@+id/offer_price"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:textColor="@color/ica_red"
        app:layout_constraintBottom_toTopOf="@+id/price_comparison"
        app:layout_constraintEnd_toStartOf="@+id/offer_add_to_user"
        app:layout_constraintStart_toEndOf="@+id/offer_image"
        app:layout_constraintTop_toBottomOf="@+id/offer_title"
        tools:text="34,50 kr/kg" />

    <TextView
        android:id="@+id/price_comparison"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toTopOf="@+id/offer_detail"
        app:layout_constraintEnd_toStartOf="@+id/offer_add_to_user"
        app:layout_constraintStart_toEndOf="@+id/offer_image"
        app:layout_constraintTop_toBottomOf="@+id/offer_price"
        tools:text="Price Comparison" />

    <ImageView
        android:id="@+id/offer_image"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ContentDescription" />

    <CheckBox
        android:id="@+id/offer_add_to_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="8dp"
        android:button="@drawable/checkbox_background"
        app:layout_constraintBottom_toTopOf="@+id/offer_detail"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/offer_detail"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/price_comparison">

        <TextView
            android:id="@+id/offer_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="0dp"
            android:text="@{offer.sizeOrQuantity()}"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="200g, ICA Eko" />


        <TextView
            android:id="@+id/disclaimer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/offer_description"
            tools:text="Disclaimer" />

    </android.support.constraint.ConstraintLayout>


</android.support.constraint.ConstraintLayout>