Tuan Nguyen
Tuan Nguyen

Reputation: 35

Constantly updating RecyclerView items

I have a RecyclerView which contains some items. Each item displays an event and it will have a text view representing the difference in time between the current time and the event time. I want to constantly update the items every second or minute (just need to rebind the item to the view holder). I am using the following code but it does not update all the items (just some of them and the updated items even flash which do not look good).

LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
Log.d(TAG, "Updating clock: " + firstVisibleItem + ", " + lastVisibleItem);
if (firstVisibleItem > 0 && lastVisibleItem > 0) {
      for (int i = firstVisibleItem; i <= lastVisibleItem; i++) {
          mAgendaAdapter.notifyItemChanged(i);
}

Is there any workaround for this problem?

Upvotes: 1

Views: 591

Answers (2)

Omar HossamEldin
Omar HossamEldin

Reputation: 3111

Here you just notify the visible items but we need to notify all. Just update the List which is used in the Adapter to bind data.

mAgendaAdapter.notifyDataSetChanged()

Or you can track the changed items and loop through them and call

mAgendaAdapter.notifyItemChanged(i);

Upvotes: 1

Matthew Shearer
Matthew Shearer

Reputation: 2795

the flash you see is the default animation for the item change. you should be able to disable it with something like -

recyclerView.getItemAnimator().setSupportsChangeAnimations(false);

if you want all items to update can you just call

mAgendaAdapter.notifyDataSetChanged()

Upvotes: 2

Related Questions