【问题标题】:Remove item and don't leave space in GridLayoutManager Recyclerview删除项目并且不要在 GridLayoutManager Recyclerview 中留下空间
【发布时间】:2018-08-27 09:26:01
【问题描述】:

我正在为我的 recyclerview 使用跨度计数为 2 的 GridLayoutManager。我试过用。

itemView.setVisibility(View.GONE);
itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0)); 

但它在回收站视图中留下了一个空间。那么有没有办法隐藏和移除recyclerview里面的空间呢?

编辑 (附加代码) 我正在使用 FirebaseRecyclerAdapter 来显示所有数据。

private ArrayList<String> mWorldGroupKeys;

@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Groups model) {
    String groupKeys = getRef(position).getKey();

    if (!mWorldGroupKeys.contains(groupKeys)) {
        holder.layoutHide(position);
        return;
    }

    holder.bind(model);
}

class ViewHolder extends RecyclerView.ViewHolder {

      void layoutHide(int pos) {

          notifyItemRemoved(pos);
          //Gave me an error of:
          //IllegalStateException: Cannot call this method while 
          //RecyclerView is computing a layout or scrolling
          //android.support.v7.widget.RecyclerView
       }
}

【问题讨论】:

  • notifyItemRemoved().
  • 我试过了,但它给了我一个错误
  • 然后在您的问题中发布该错误
  • 嗨,您正在尝试隐藏该项目,您可以做的是从列表中删除该项目,然后 notifyItemRemoved(); 例如:list.remove(position);
  • if (!mWorldGroupKeys.contains(groupKeys)) { mWorldGroupKeys.remove(position);notifyItemRemoved(position) return; } 试试这个

标签: java android android-recyclerview gridlayoutmanager


【解决方案1】:

从适配器上的列表中删除项目后,您可以使用notifyItemRemoved();notifyDataSetChanged();。这 2 个函数将刷新列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多