[英]Update RecyclerView adapter item without calling OnCreateViewHolder
how to update ReyclerView adapter without make OnCreateViewHolder(...) being called? 如何在不调用OnCreateViewHolder(...)的情况下更新ReyclerView适配器?
Because when i do 因为当我做
mRecyclerView.getAdapter().notifiItemChanged(position)
it goes inside OnCreateViewHolder(...)
what i don't want to. 它进入OnCreateViewHolder(...)
我不想要的。 The reason why i don't want is because I have expanded item view and onCreateViewHolder(...)
will reinflate it so make collapsed. 我不想要的原因是因为我已经展开了项目视图,并且onCreateViewHolder(...)
将重新膨胀它,因此使其折叠。 I only want to OnBindViewHolder(...)
being called 我只想OnBindViewHolder(...)
Have anyone faced with it? 有人面对过吗?
UPD: Just found that OnCreateViewHolder(...)
is called only one first time, in all next times it's not being called. UPD:刚发现OnCreateViewHolder(...)
第一次仅被调用,而在以后的所有时间中均未被调用。 What is the reason? 是什么原因? t Ť
This is the whole point of RecyclerView
: it handle a pool of view holders, which are created once (calling OnCreateViewHolder()
) then reused (or recycled, hence the view name) as much as possible via OnBindViewHolder()
. 这就是RecyclerView
:它处理视图持有者池,该视图持有者创建一次(调用OnCreateViewHolder()
),然后通过OnBindViewHolder()
尽可能地重用(或回收,因此使用视图名称OnBindViewHolder()
。
OnCreateViewHolder()
is used to create the views, while OnBindViewHolder
(re)set the views state (like text content, selected state, ...) OnCreateViewHolder()
用于创建视图,而OnBindViewHolder
(重新)设置视图状态(如文本内容,选定状态等)。
It follows that any state associated with an item (like the expanded / collapsed state) should not be stored in the ViewHolder
. 因此,与项目关联的任何状态(如展开/折叠状态)都不应存储在ViewHolder
。 You can for example use a private List in the adapter. 例如,您可以在适配器中使用私有列表。
You could add a boolean isExpanded
to your data objects in your dataset, and then in the onBindViewHolder
method check whether isExpanded
is true
, if so expand the view programmatically. 您可以在数据集中的数据对象中添加一个boolean isExpanded
,然后在onBindViewHolder
方法中检查isExpanded
是否为true
,如果这样可以以编程方式扩展视图。 (and ofcourse don't forget to switch the boolean
when expanding/collapsing the view) (当然,在展开/折叠视图时,请不要忘记切换boolean
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.