ListAdapter not refreshing RecyclerView if I submit the same list with different item order

后端 未结 5 1791
挽巷
挽巷 2021-01-12 06:57

I am using a RecyclerView with ListAdapter (which uses AsyncListDiffer to calculate and animate changes when list is replaced).

The problem is that if I

5条回答
  •  悲&欢浪女
    2021-01-12 07:44

    It defeats ListAdapter's purpose for automatically calculating and animating list changes when you call these lines consecutively:

    submitList(null);
    submitList(orderChangedList);
    

    Meaning, you only cleared (null) the ListAdapter's currentList and then submitted ( .submitList()) a new List. Thus, no corresponding animation will be seen in this case but only a refresh of the entire RecyclerView.

    Solution is to implement the .submitList( List list) method inside your ListAdapter as follows:

    public void submitList(@Nullable List list) {
        mDiffer.submitList(list != null ? new ArrayList<>(list) : null);
    }
    

    This way you allow the ListAdapter to retain its currentList and have it "diffed" with the newList, thereby the calculated animations, as opposed to "diffing" with a null.

    Note: However, no update animation will happen, if, of course, the newList contains the same items in the same order as the originalList.

提交回复
热议问题