Why RecyclerView.notifyItemChanged() will create a new ViewHolder and use both the old ViewHolder and new one?

后端 未结 6 556
半阙折子戏
半阙折子戏 2020-12-18 17:57

Recently I use RecyclerView and add a custom header view (another type of item view) and try to updated it when data has changed. Something strange happens. The adapter crea

6条回答
  •  执念已碎
    2020-12-18 18:05

    Here are a few issues with your implementation:

    • getItemCount expects the count of all items in the recyclerview including the header so you should return mItemList.size() + 1

    • the position field in onBindViewHolder() refers to the position of an element in the whole recyclerview including the header. so to bind a non-header item you will do something like item = mItemList.get(position - 1) -- this won't fail because getItemViewType returns a number greater than 0 for TYPE_ITEMs

    By doing so, notifyItemChanged should behave as expected

提交回复
热议问题