【问题标题】:ConstraintLayout View Gone Keeping SpaceConstraintLayout View Gone 保留空间
【发布时间】:2020-01-25 13:08:55
【问题描述】:

我的回收站视图有以下约束布局。我根据我的 recyclerview 适配器中的数据库值隐藏并显示它。它的工作但问题是视图消失后它仍然保持占用的空间。下面是我的xml文件

<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:id="@+id/books_layout" // show-hide based on this id 
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/imgAvator"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tvV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:text=""
    android:textSize="16dp"
    app:layout_constraintStart_toEndOf="@+id/imgAvator"
    app:layout_constraintTop_toBottomOf="@+id/tvDriverNme" />

<TextView
    android:id="@+id/tvD"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="2dp"
    android:text=""
    android:textColor="#050505"
    android:textSize="18dp"
    app:layout_constraintStart_toEndOf="@+id/imgAvator"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tvR"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginRight="5dp"
    android:text=""
    android:textColor="#FF9800"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<TextView
    android:id="@+id/tvB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="21dp"
    android:text=""
    android:textColor="#050505"
    android:textSize="16dp"
    android:textStyle="bold"
    app:layout_constraintEnd_toStartOf="@+id/tvTk"
    app:layout_constraintTop_toBottomOf="@+id/tvRatings" />

<TextView
    android:id="@+id/tvReg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="45dp"
    android:layout_marginEnd="5dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/strike_through"
    android:text=""
    android:textColor="#707676"
    android:textSize="16dp"
    app:layout_constraintEnd_toStartOf="@+id/tvBidAmt"
    app:layout_constraintTop_toTopOf="parent" />

 </android.support.constraint.ConstraintLayout>

我正在使用我的数据绑定中的以下代码显示/隐藏

 if (status.equals("1")) {
    books_layout.setVisibility(View.GONE);
 }
 else{
    books_layout.setVisibility(View.VISIBLE);
 } 

下面是我当前的视图,它展示了视图消失后中间占用的空间。我不知道如何解决这个问题

【问题讨论】:

    标签: android android-recyclerview android-constraintlayout


    【解决方案1】:

    如果您不想删除项目,也可以将项目的高度设置为 0。此外,您不需要 id。您可以直接拨打itemViewitemView 是您在 onCreateViewHolder 中膨胀的那个布局的膨胀视图。

    如何将高度设置为 0:

    ViewGroup.LayoutParams params = itemView.getLayoutParams();
     if (status.equals("1")) {
        params.height = 0;
        itemView.setLayoutParams(params);
     }
     else{
        params.height = WRAP_CONTENT;
        itemView.setLayoutParams(params);
     } 
    

    【讨论】:

      【解决方案2】:

      布局正常。

      你的问题是你的回收视图items。它的item占用空间。

      如果你想删除回收视图中的占用空间。您必须删除回收视图项。

      【讨论】:

        【解决方案3】:

        如果我没记错的话,你在任何适配器中都使用了这个布局。这意味着你的程序中使用了 Listview 或 recyclerview,如果这是正确的,那么你隐藏你的 itemView。就像这个 holder.itemView。在 bindView() 中的 setVisibily()。

        【讨论】:

          猜你喜欢
          • 2019-02-27
          • 2017-09-26
          • 1970-01-01
          • 2023-03-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-11
          • 2011-01-30
          相关资源
          最近更新 更多