简体   繁体   English

Recyclerview调用notifyItemChanged导致空白项目

[英]Recyclerview calls notifyItemChanged to cause a blank item

RecyclerView calls notifyItemChanged() to cause a blank entry RecyclerView调用notifyItemChanged()导致空白条目

I have a RecyclerView and it's every child item has a RecyclerView , when the dataset of RecyclerView in child item has changed (eg.some item inserted). 我有一个RecyclerView和它的每一个子项目有RecyclerView ,当数据集RecyclerView在子项已经改变(eg.some项目插入)。 I try to call the parent RecyclerView 's notifyItemChanged() to refresh it. 我尝试调用父RecyclerViewnotifyItemChanged()刷新它。 But when I call that function, it will cause a blank item below. 但是,当我调用该函数时,它将导致下面的空白项。

mMessagesAdapter.notifyItemChanged(position);

when I call notifyItemChanged() for a position, there will be a blank item below, it can be visible, but there is no data, and it's all white. 当我为某个位置调用notifyItemChanged() ,下面将有一个空白项目,它是可见的,但是没有数据,并且全为白色。

Demo: https://github.com/zoodeveloper/NestBug 演示: https : //github.com/zoodeveloper/NestBug

The adapter.notifyItemChanged () is used for refreshing an existing item. adapter.notifyItemChanged ()用于刷新现有项目。 If you are adding a new item, then you should call adapter.notifyItemRangeChanged(position, data.size()) 如果要添加新项目,则应调用adapter.notifyItemRangeChanged(position, data.size())

If you are inserting a new item you can use notifyItemInserted (int position) 如果要插入新项目,则可以使用notifyItemInserted (int position)

It is used to notify any registered observers that the item reflected at position has been newly inserted. 它用于通知任何注册的观察者该位置反射的项目已被新插入。 The item previously at position is now at position position + 1. 先前在该位置的项目现在在位置位置+1。

Maybe you should consider to call adapter.notifyItemChanged() from the main thread. 也许您应该考虑从主线程调用adapter.notifyItemChanged()。

Handler mainHandler = new Handler(Looper.getMainLooper());

Runnable myRunnable = new Runnable() {
   @Override 
   public void run() { adapter.notifyItemChanged() } 
};
mainHandler.post(myRunnable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RecyclerView Adapter notifyItemChanged触发两次 - RecyclerView Adapter notifyItemChanged triggers twice notifyItemChanged(int)不会更新RecyclerView.Adapter - notifyItemChanged(int) does not update RecyclerView.Adapter RecyclerView notifyItemChanged(position)使应用程序崩溃,并且使用notifyDataSetChanged()没有动画 - RecyclerView notifyItemChanged(position) crashes the app and no animations with notifyDataSetChanged() Recyclerview 在 Android 中的 notifyItemChanged 后防止触摸事件 - Recyclerview preventing touch events after notifyItemChanged in Android Android RecyclerView Adapter:如何实现 notifyItemChanged 线程安全? - Android RecyclerView Adapter: how to implement notifyItemChanged thread safety? 在使用layoutManager的scrollToPositionWithOffset时,项目的recyclerView之间为空白 - Blank between item's recyclerView when scrollToPositionWithOffset with layoutManager RecyclerView:检测到不一致。 无效的项目位置。 原因 - 使用计时器删除项目 - RecyclerView: Inconsistency detected. Invalid item position. Cause -Removing item using timer Recyclerview项目中的Android recyclerview - Android recyclerview in a recyclerview item RecyclerView在片段内为空白 - RecyclerView is blank inside a fragment RecyclerView空白页 - RecyclerView blank page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM