Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

android - Force RecyclerView to redraw its items

Is there any way to redraw all items of RecyclerView?

I have some Themes (in style.xml) and after changing the theme, I need the RecyclerView to be redrawn.

So I want a method that will force to re-call onCreateViewHolder for each items of the adapter.

I tried to:

  • call adapter.notifyDataSetChanged but onCreateViewHolder is not called
  • call recyclerView.setVisibility(View.GONE) and then recyclerView.setVisibility(View.VISIBLE)
  • call recyclerView.invalidate()
  • call recyclerView.setAdapter(null) and then recyclerView.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

I mention that the RecyclerView is attached to an Activity, not to a Fragment.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...