我在回收者视图的约束布局下面。我正在隐藏并根据我的recyclerview Adapter中的Database值显示此文件。它可以正常工作,但是问题是在视图消失后它仍然保留了占用的空间。以下是我的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);
}
下面是我当前的视图,显示视图消失后中间的占用空间。我不知道如何解决此问题
布局工作正常。
您的问题是您的回收视图项目。该项目已占用空间。
如果要在回收视图中删除占用的空间,请执行以下操作:您必须删除回收视图项目。
如果我没记错,您在任何适配器中都使用了此布局。这意味着程序中使用了Listview或recyclerview,如果正确,则将您的itemView隐藏起来。就像这个holder.itemView.setVisibily()一样bindView()。
如果您不想删除该项目,也可以将项目的高度设置为0。此外,您不需要ID。您可以直接拨打itemView
。 itemView
是您在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);
}