在视图中滚动时,recyclerView不会调用onBindViewHolder [英] recyclerView does not call the onBindViewHolder when scroll in the view

查看:990
本文介绍了在视图中滚动时,recyclerView不会调用onBindViewHolder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是注意到的: 启动时,数据项索引0、1,...,5会显示在视图中.并且看到onCreateViewHolder()和onBindViewHolder()被要求为它们调用. 然后滚动出最上面的项目,并看到为索引6调用了onCreateViewHolder()和onBindViewHolder(). 然后向下滚动以使项目索引0重新显示.由于未回收,因此不会调用onBindViewHolder().

This is what noticed: When it starts, the data item index 0, 1, ..., 5 are displayed in the view. And saw the onCreateViewHolder() and onBindViewHolder() got called for them. Then scroll out the top item, and saw the the onCreateViewHolder() and onBindViewHolder() got called for the index 6. Then scroll down to bring back the item index 0 into view. Since it was not recycled so no onBindViewHolder() is called.

这是设计使然,但有一个案例希望调用onBindViewHolder.

This is by design, but having a case would like the onBindViewHolder to be called.

当项目索引0不在视图中时,单击视图中的任何项目都想更改项目索引0的数据.滚动回视图时,将显示数据的更改.但是由于未调用其onBindView,因此该行的数据更改不会在UI中更新.

When item index 0 is out the view, and clicking on any item in the view would like to change the item index 0's data. When it is scroll back into the view would like to show the data's change. But since its onBindView is not called, the data change of this row is not updated in UI.

它可以呼叫

notifyDataSetChanged() 

在更改数据后强制重新绘制列表.但是结果不佳,因为视图项上有图像并且显示闪烁.

after the data change to force redraw the list. But the result is not good because there is image on the view item and shows flickering.

尝试使用LayoutManager获取强制更新的视图.问题是什么时候才是最好的时间知道索引0正在LayoutManager的子视图中显示?

tried use LayoutManager to get whatever is the view to force update. The problem is when is best time to know that index 0 is showing up in the LayoutManager's children view?

有什么建议吗? 谢谢!

Any suggestion? Thanks!

推荐答案

我找到了

onViewAttachedToWindow(ViewHolder viewHolder)

可以用于这种情况.每当在视口中显示viewHolder时,都会调用它,因此在其中会根据当前数据的状态更新视图.

could be used for this case. Whenever a viewHolder is shown in the viewport it is called, so in it to update the view according to current data's state.

这篇关于在视图中滚动时,recyclerView不会调用onBindViewHolder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆