【问题标题】:How to remove empty space left after deleting items in a recycler view for android?如何删除在 android 的回收站视图中删除项目后留下的空白空间?
【发布时间】:2017-05-02 23:43:33
【问题描述】:

我有一个非常简单的recyclerview,布局如下:-

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

我的回收站视图布局如下:-

<?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"
    />

这是我用来从回收站视图中删除项目的代码:-

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

我的线性布局在滚动视图内。现在,当我在回收站视图中删除项目时,该项目被删除,但删除后留下空白空间。当回收站视图内的项目时,如何自动删除该空间被删除了?

感谢任何帮助或建议。谢谢。

【问题讨论】:

  • 调试和检查删除项目后列表大小是否减一。并尝试 notifyDataSetChanged()

标签: java android xml layout android-recyclerview


【解决方案1】:

尝试添加 notifyItemRangeChanged(int, int) 在 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());
}

【讨论】:

  • 我尝试了你的解决方案,但删除后我仍有空白空间?
  • recyclerview 项目布局是什么样的?你的适配器和查看器是什么样的?
【解决方案2】:

也许您将 setHasFixedSize 设置为 true,所以尝试将其设置为 false setHasFixedSize(false),它会删除该空间

【讨论】: