Force RecyclerView to redraw its items

空扰寡人 提交于 2019-12-03 01:04:22

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!

Simply setting recyclerview's adapter again worked for me (I wanted RecyclerView to redraw all items' layout again)

/**
 * Forces RecyclerView adapter to call createViewHolder() - i.e. redraw all all items' layouts
 */
private fun resetAdapterState() {
    val myAdapter = recyclerView.adapter
    recyclerView.adapter = myAdapter
}
Oleg Kovalyk

If you want to force redraw, you need clear View Pool of RecycleView. You can use recyclerView.getRecycledViewPool().clear();

The below lines of code did the trick for me

recyclerview.swapAdapter(myAdapter,false);
recyclerview.setLayoutManager(myLayoutManager);
myAdapter.notifyDataSetChanged();

The only solution that worked for me on an Amazon Fire HD was this:

recyclerView.setAdapter(null);
recyclerView.setLayoutManager(null);
recyclerView.getRecycledViewPool().clear();
recyclerView.swapAdapter(myAdapter, false);
recyclerView.setLayoutManager(layoutManager);
myAdapter.notifyDataSetChanged();

Hope it helps!

Anton Shkurenko

Take a look at this answer: https://stackoverflow.com/a/33342314/4142087

Shortly, you can create different types of view holders, changing view type will force RecyclerView to pass another ViewHolder to the onBindViewHolder.

If you use setTheme, you have to recreate whole Activity like this: https://stackoverflow.com/a/14367214/4142087

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!