recyclerView does not call the onBindViewHolder when scroll in the view

早过忘川 提交于 2019-12-24 05:06:09

问题


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.

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

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.

It could call

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.

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!


回答1:


I found the

onViewAttachedToWindow(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.



来源:https://stackoverflow.com/questions/32913578/recyclerview-does-not-call-the-onbindviewholder-when-scroll-in-the-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!