I have a RecyclerView that displays items with a title, text and last edit time.
In the RecyclerView adapter's onBindViewHolder I use getRelativeTimeSpanString
and compare the note object's time to current time:
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Note note = getItem(position);
holder.title.setText(note.getTitle());
holder.text.setText(note.getText());
String s = DateUtils.getRelativeTimeSpanString(note.getTime(),
System.currentTimeMillis(), 0L, DateUtils.FORMAT_ABBREV_ALL).toString();
holder.timestamp.setText(s);
}
If I just added the item, it says "0 sec ago". How do I update that over time?
While writing this question, SO suggested this question: Updating the relative time in android , however I don't think updating the RecyclerView every X seconds is the the best idea:
- it drains battery
- it updates items that don't need updating (e.g.,
last update was 6 hours ago, but it's updating
holder.timestamp
every X seconds)
Is there a more efficient way to accomplish this?
Given you only need to update the UI when it is visible, you can reduce battery drain by only updating your UI when you know it's in the foreground using the
onResume()
andonPause()
lifecycle callbacks in your Fragment/Acitvity. In addition, if minute granularity is good enough for your use case, you can use theACTION_TIME_TICK
system broadcast, which is fired every one minute, so you don't need to create your own timer needlessly.First, register a
BroadcastReceiver
to listen for this broadcast inonResume()
, and unregister inonPause()
:Inside
receiver
, contain the logic for updating the relevant items of yourRecyclerView
(or delegate it to yourAdapter
, as a rough example: