anup
anup

Reputation: 605

How to remove empty space left after deleting items in a recycler view for android?

I have a very simple recyclerview with the following layout:-

  <LinearLayout
            android:id="@+id/address_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <include layout="@layout/address_recyclerview"></include>

        </LinearLayout>

Wher my recycler view layout is as follows:-

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/adr_recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    />

This is the code I am using to delete the item from the recycler view:-

 private void removeItem(UserInfo list) {
        int current_position = listData.indexOf(list);
        listData.remove(current_position);
        notifyItemRemoved(current_position);
    }

My Linear Layout is within a Scroll View.Now when I delete a item inside the recycler view the item gets deleted but after the deletion empty space is left behind.How do I auto delete this space when the item inside the recycler view is deleted?

Any help or suggestion is appreciated.Thank you.

Upvotes: 1

Views: 2127

Answers (2)

Seddik Fredj
Seddik Fredj

Reputation: 133

Maybe you set setHasFixedSize to true, so try to make it false setHasFixedSize(false) and it will remove that space

Upvotes: 2

julamme
julamme

Reputation: 48

Try adding notifyItemRangeChanged(int, int) after notifyItemRemoved(current_position).

 private void removeItem(UserInfo list) {
    int current_position = listData.indexOf(list);
    listData.remove(current_position);
    notifyItemRemoved(current_position);
    notifyItemRangeChanged (current_position, getItemCount());
}

Upvotes: 2

Related Questions