ListView not refreshing already-visible items

后端 未结 3 1986
长情又很酷
长情又很酷 2020-12-03 05:23

I\'m displaying a list of contacts (name + picture) using the ListView. In order to make the initial load fast, I only load the names first, and defer picture l

3条回答
  •  一向
    一向 (楼主)
    2020-12-03 06:00

    According to the documentation:

    void notifyDataSetChanged ()

    Notify any registered observers that the data set has changed. ... LayoutManagers will be forced to fully rebind and relayout all visible views...

    In my case, the items were not visible (then whole RecycleView was outside the screen), and later on when it animated in, the item views didn't refresh either (thus showing the old data).

    Workaround in the Adapter class:

    public void notifyDataSetChanged_fix() {
        // unfortunately notifyDataSetChange is declared final, so cannot be overridden. 
        super.notifyDataSetChanged();
        for (int i = getItemCount()-1; i>=0; i--) notifyItemChanged(i);
    }
    

    Replaced all calls of notifyDataSetChanged() to notifyDataSetChanged_fix() and my RecyclerView happily refreshing ever since...

提交回复
热议问题