Edition
Looking for more solutions that do not work well, since they create memory leaks etc. I found that component link Works perfectly in a recyclerview, missing items overload check 1000 items to determine if it is fine or not.
Its implementation:
<com.github.curioustechizen.ago.RelativeTimeTextView
android:id="@+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:relative_time_prefix="Completed "
android:layout_marginTop="@dimen/margin_primary" />
Assignment of a previous saved time onBindViewHolder
if (item.getLastChecked() != null)
holder.mSummary.setReferenceTime(item.getLastChecked().getTime());
else
//sin tiempo especificado
Previous
Following that example: link
I created a class UpdateTimerThread
that implements Runnable
public class UpdateTimerThread implements Runnable {
private static final String TAG = UpdateTimerThread.class.getSimpleName();
public TextView holder;
Handler handler;
public UpdateTimerThread(Handler handler, TextView holder) {
Log.d(TAG, "CREATE UpdateTimerThread: ");
this.handler = handler;
this.holder = holder;
}
@Override
public void run() {
/* do what you need to do */
Date date = new Date();
String stringDate = DateFormat.getDateTimeInstance().format(date);
holder.setText(stringDate);
Log.d(TAG, stringDate);
handler.postDelayed(this, 100);
}
}
Then in the allocation of ViewHolder
is where the counter starts
public class SimpleListAdapter
extends RecyclerView.Adapter<SimpleListAdapter.ViewHolder> {
...
private Handler handler = new Handler();
...
public class ViewHolder extends RecyclerView.ViewHolder {
...
UpdateTimerThread customRunnable;
...
public ViewHolder(View v) {
super(v);
customRunnable = new UpdateTimerThread(handler,mSummary);
handler.postDelayed(customRunnable, 100);
}
Create function to finish the counters when pausing the activity
public void clearAll() {
handler.removeCallbacksAndMessages(null);
}
In the Activity
@Override
protected void onPause() {
super.onPause();
mAdapter.clearAll();
}
Problems that I detect: that executes a thread for each element that is displayed, without paying attention that if it is destroyed stop the counter, that is if you have 1000 items and you go through the list, a thread is executed for each item that has been shown, only when you turn the terminal is when you delete them.
If an item is deleted, then its execution thread is still there