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?