本文介绍了强制 RecyclerView 重绘其项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法重绘RecyclerView
的所有items?
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
附加到一个 Activity,而不是一个 Fragment.
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 重绘其项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!