
Recycler view ile listenizde gönderdiğiniz saat değerinizi kullanıp birden çok aynı anda veya farklı olan sayaç yapacaksanız bu kod işinizi görecektir.
Örnek Kısa Kod:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public void onBindViewHolder(final FeedViewHolder holder, final int position) { ... if (holder.timer != null) { holder.timer.cancel(); } holder.timer = new CountDownTimer(expiryTime, 500) { ... }.start(); } public static class FeedViewHolder extends RecyclerView.ViewHolder { ... CountDownTimer timer; public FeedViewHolder(View itemView) { ... } } |
Açıklamak gerekirse Adapter yapınızda kullanacağınız kod aşağıda gibidir.
onBindViewHolder içerisinde çalıştıracağımız kısım.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//Gün saat dakika saniye değerlerini alıyoruz ve bir timers değeri oluşturuyoruz long timers=0; timers=((iDays*24+iHrs)*60+iMins)*60+iSecs; Log.d("AdapterDate","--"+timers); if (h.countDownTimer != null) { h.countDownTimer.cancel(); } h.countDownTimer=new CountDownTimer(timers*1000,1000){ @Override public void onTick(long l) { h.tv_dark_dateCount.setText(formatMilliSecondsToTime( l)); //Log.d("AdapterDate",position+"--"+l); } @Override public void onFinish() { h.tv_dark_dateCount.setText("You missed this one!"); } }.start(); |
Adapter içinde method olarak tanımladığımız kısım
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public String formatMilliSecondsToTime(long milliseconds) { dates=""; int seconds = (int) (milliseconds / 1000) % 60; int minutes = (int) ((milliseconds / (1000 * 60)) % 60); int hours = (int) ((milliseconds / (1000 * 60 * 60)))%24; int days = (int) ((milliseconds / (1000 * 60 *60*24))); if(days>0) dates+=twoDigitString(days)+" Days "; if(days>0 ||hours>0) dates+=twoDigitString(hours) + " Hrs "; if(minutes>=0) dates+=twoDigitString(minutes) + " Mins "+ twoDigitString(seconds)+" Secs"; return String.valueOf(dates); } private String twoDigitString(long number) { if (number == 0) { return "00"; } if (number / 10 == 0) { return "0" + number; } return String.valueOf(number); } |
Bu kadar.
Bir yanıt bırakın