mco
mco

Reputation: 1839

RecyclerView Adapter's notifyItemRangeChanged is slow?

I'm trying to update a range of items in my RecyclerView. Basically I'm toggling a view between visible/gone for every item in my list. I'm trying to use notifyItemRangeChanged, but it's very slow and there is noticeable amount of UI lag. My onBindViewHolder isn't super complex either. Is there something I'm supposed to do in the background thread for this?

// Called in Activity
mListAdapter.notifyItemRangeChanged(0, mPlayerlist.size());


// My adapter's onBindViewHolder method
@Override
public void onBindViewHolder(@NonNull PlayerHolder holder, int position) {
    Log.d("onBind", position + "");
    Player player = mPlayerList.get(position);

    if (player.isCheckboxVisible()) {
        holder.checkbox.setVisibility(View.VISIBLE);

        if (player.isIncluded()) {
            holder.checkbox.setChecked(true);
        } else {
            holder.checkbox.setChecked(false);
        }
    } else {
        holder.checkbox.setVisibility(View.GONE);
    }

    holder.playerNameText.setText(player.getName());
}

More info as I ran some tests: onBindViewHolder takes about 0-2 ms each, and there are about 5-10 items shown depending on phone size. onBindViewHolder itself looks like it executing pretty fast, but the time for all onBindViewHolder to finish executing in succession is slow. It took .7 seconds for 14 onBindViewHolders to complete. There is like a delay between the onBindViewHolder calls.

Upvotes: 0

Views: 1641

Answers (2)

mco
mco

Reputation: 1839

I just figured out the reason why it's so slow is because notifyItemRangeChanged causes too many item change animations to run at the same time.

Calling recyclerView.setItemAnimator(null) makes the call quick and snappy now. Downside is I lose the animations now... =/

Upvotes: 3

Akshay Khajuria
Akshay Khajuria

Reputation: 123

I think you can use mListAdapter.notifyDataSetChanged();

Upvotes: 0

Related Questions