强制RecyclerView重绘其项目 [英] Force RecyclerView to redraw its items
本文介绍了强制RecyclerView重绘其项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么办法可以重新绘制RecyclerView
的所有项目?
Is there any way to redraw all items of RecyclerView
?
我有一些主题(在style.xml中),更改主题后,需要重绘RecyclerView
.
I have some Themes (in style.xml) and after changing the theme, I need the RecyclerView
to be redrawn.
所以我想要一个方法,该方法将强制为适配器的每个项目重新调用onCreateViewHolder
.
So I want a method that will force to re-call onCreateViewHolder
for each items of the adapter.
我试图:
- 调用
adapter.notifyDataSetChanged
,但未调用onCreateViewHolder
- 先致电
recyclerView.setVisibility(View.GONE)
,然后再致电recyclerView.setVisibility(View.VISIBLE)
- 致电
recyclerView.invalidate()
- 先呼叫
recyclerView.setAdapter(null)
,然后再呼叫recyclerView.setAdapter(adapter)
.
这对于90%的项目效果很好.只有90%的商品会使用新样式,但有些商品会保留旧样式
- call
adapter.notifyDataSetChanged
butonCreateViewHolder
is not called - call
recyclerView.setVisibility(View.GONE)
and thenrecyclerView.setVisibility(View.VISIBLE)
- call
recyclerView.invalidate()
- call
recyclerView.setAdapter(null)
and thenrecyclerView.setAdapter(adapter)
.
This works well for 90% items. Only 90% of items will get the new style, but some items will have the old style
我提到RecyclerView
是附加到活动上的,而不是附加到片段上的.
I mention that the RecyclerView
is attached to an Activity, not to a Fragment.
推荐答案
我找到了答案! 正确的方法是:
I found the answer! The correct way to do this is:
recyclerView.setAdapter(null);
recyclerView.setLayoutManager(null);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(myLayoutManager);
myAdapter.notifyDataSetChanged();
在那之后,所有的物品都变了新样式!
After that, all the items are getting the new style!
这篇关于强制RecyclerView重绘其项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文