Mithu
Mithu

Reputation: 655

ConstraintLayout View Gone Keeping Space

I have below Constraint Layout for my recycler view. I am hiding and showing this based on Database value from my recyclerview Adapter.Its working but problem is after view gone it is still keeping the occupied space. Below is my xml file

<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>

I am show/hiding with below code from my databind

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

Below is my current view which demonstrates the occupied space in the middle after view gone.I have no idea how to resolve this issue

enter image description here

Upvotes: 1

Views: 1225

Answers (3)

Loremar Marabillas
Loremar Marabillas

Reputation: 833

If you don't want to delete the item, you could also set the item's height to 0. Also, you don't need the id. You can just directly call itemView. itemView is the inflated view of that layout which you inflated in onCreateViewHolder.

How to set height to 0:

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

Upvotes: 1

A Farmanbar
A Farmanbar

Reputation: 4788

Layout working correct.

your problem is your recycle view items.it's item occupied space.

if you want to remove occupied space in recycle view . you have to delete recycle view item.

Upvotes: 1

ashok
ashok

Reputation: 429

If I am not wrong ,You used this layout in any adapter.It means Listview or recyclerview used in you program , If this is right then you hide your itemView .Like this holder.itemView.setVisibily() inside bindView().

Upvotes: 0

Related Questions