S
S
Shohrukh Alimov2017-05-19 12:19:59
Android
Shohrukh Alimov, 2017-05-19 12:19:59

How to add CountDownTimer to RecyclerView items?

I need to show CountDown on every element. this is how i do it now

@Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final PromosGS promos = promos_List.get(position);
         
        CountDownTimer timer = new CountDownTimer(total_millis, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    long days = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
                    millisUntilFinished -= TimeUnit.DAYS.toMillis(days);

                    long hours = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
                    millisUntilFinished -= TimeUnit.HOURS.toMillis(hours);

                    long minutes = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
                    millisUntilFinished -= TimeUnit.MINUTES.toMillis(minutes);

                    long seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
                    holder.promoDate.setText(days + "д" + " " + hours + "ч" + " " + minutes + "м" + " " + seconds + "с");
                }

                @Override
                public void onFinish() {
                    holder.promoDate.setText("Завершен");
                }
            };
}

Everything shows fine, but when updating or "onPause", the timer does not stop and in each element it doubles (this is only visible in the log).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2017-05-19
@AlimNinja

and why the timer should stop, someone stops it?
and I'm not sure that the CountDownTimer uses the thread pool, I believe that it starts a new thread when it is created. if I were you, I would have one handler that would tick and run only through the visible elements of the list and update the necessary data for them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question