Force RecyclerView to redraw its items

后端 未结 9 1163
闹比i
闹比i 2021-02-02 06:45

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 t

相关标签:
9条回答
  • 2021-02-02 07:17

    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!

    0 讨论(0)
  • 2021-02-02 07:20

    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!

    0 讨论(0)
  • 2021-02-02 07:25

    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
    }
    
    0 讨论(0)
  • 2021-02-02 07:30

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

    0 讨论(0)
  • 2021-02-02 07:30

    I know this is old, but I had a situation where I needed to change the viewholder types after notifyDataSetChanged(). If the adapter already had data, onCreateViewHolder was not being called because of the "recycle" part of recycleviewadapter, so a type cast error was being thrown in onBindViewHolder.

    I was able to solve this by calling myRecyclerView.getLayoutManager().removeAllViews(); before calling notifyDataSetChanged(); on the adapter.

    0 讨论(0)
  • 2021-02-02 07:31

    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

    0 讨论(0)
提交回复
热议问题