Search code examples
androidandroid-recyclerviewandroid-adapternotifydatasetchanged

RecyclerView - control item count by boolean


I'm using a RecyclerView where there is a boolean involved in getItemCount(). This boolean value controls the visibility of the last item in the RecyclerView, kinda like a load more view, but slightly different. From time to time, the value of this boolean changes, so the RecyclerView will know whether to include the last item.

The code looks something like this

@Override
public int getItemCount() {
    return mItemArray.size() + (shouldShow ? 1 : 0); 
}

When the value of shouldShow gets updated, I'm not calling notifyDataSetChanged() or notifyItemChanged(), but the RecyclerView still gets updated properly. But I worry it will eventually cause the RecyclerView to crash because of inconsistency. My questions are,

  1. Is using a dynamic boolean value in getItemCount() a proper way to control RecyclerView size?
  2. Should I call notify...() when the value of this boolean changes?
  3. Would my RecyclerView crashes for inconsistency because of this?

Solution

  • 1.not a problem untill you are passing int in it 2.there is no need to call notifydatasetchanged(we should call them when their is change in adapter data) 3.there will not be any consistency