简体   繁体   English

更新RecyclerView适配器项而不调用OnCreateViewHolder

[英]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.

相关问题 RecyclerView onCreateViewHolder 方法未调用 - RecyclerView onCreateViewHolder method not calling RecyclerView 不调用 onCreateViewHolder 或 onBindView - RecyclerView not calling onCreateViewHolder or onBindView RecyclerView中没有调用适配器的onCreateViewHolder和onBindViewHolder方法? - Adapter onCreateViewHolder and onBindViewHolder methods are not getting called in RecyclerView? Recyclerview 不调用任何适配器方法 :onCreateViewHolder,onBindViewHolder, - Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder, 如何在不调用 notifyItemChanged 或 DiffUtil 的情况下更新/刷新 RecyclerView 的特定项目? - How to update/refresh a specific item of RecyclerView without calling notifiyItemChanged or DiffUtil? 如何更新 recyclerview 适配器中的特定项目范围 - How to update specific item range in recyclerview adapter 通知适配器插入 RoomDatabase 中的项目并更新 recyclerView - Notify adapter on item inserted in RoomDatabase and update the recyclerView 更新recyclerView适配器而不滚动到顶部 - Update recyclerView Adapter without scrolling to top Recyclerview Adapter onCreateViewHolder 方法 LinearLayout 无法转换为 TextView - Recyclerview Adapter onCreateViewHolder method LinearLayout cannot be cast to TextView 使用新创建的项目返回到以前的活动时更新 recyclerView 适配器 - Update recyclerView Adapter on return to previous activity with newly created item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM