How to handle multiple countdown timers in RecyclerView?

后端 未结 5 2211
长情又很酷
长情又很酷 2021-02-10 06:45

I have a Recyclerview , and I need to display a countdown on every row.

Here is a similar question coutndown timers in listview It has a good solution ,

5条回答
  •  太阳男子
    2021-02-10 07:15

    The Simple Solution Would Be:

    Handler handler=new Handler();
    handler.postDelayed(new UpdateTimerThread(holder),0); 
    
    public Class UpdateTimerThread implements Runnable{
    Holder holder;
    
        public UpdateTimerThread(Holder holder){
            this.holder=holder;
        }
        @Override
        public void run() {
            lgetCreatedTime = lgetCreatedTime + 1000;
            long diffSeconds;
            long diffMinutes;
            long diffHours;
            diffSeconds = lgetCreatedTime / 1000 % 60;
            diffMinutes = lgetCreatedTime / (60 * 1000) % 60;
            diffHours = lgetCreatedTime / (60 * 60 * 1000) % 24;
            holder.GameTimer.setText(String.format("%02d:%02d:%02d", diffHours,   diffMinutes, diffSeconds));
            handler.postDelayed(this,1000);
        }
    }
    

    Note: 1.holder is the object of ViewHolder class

    2.Create a class UpdateTimerThread implements Runnable

    3.Convert Date and Time to long and store into lgetCreatedTime

    4.Run Handler for every 1 Sec to tick the Time

提交回复
热议问题