I'm using android.os.CountDownTimer
in each row of a recycler view, but my app is lagging when I scroll up/down in recycler view. Should I run the timer in a separate thread? If it is correct way, then should I create a separate thread for timer or a single thread for every timer?
public class GoodkartSearchResultItemHolder extends BaseGoodkartSearchResultHolder{
//some code
private void startTimer(GetGoodkartSearchResultResponse.ProductPackagesBean productInfoBean, boolean isEndTime) {
if (productInfoBean != null) {
Date expiryTime;
if (isEndTime) {
expiryTime = new Date(productInfoBean.offerEndTime);
} else {
expiryTime = new Date(productInfoBean.offerStartTime);
}
Date currentTime = new Date();
long duration = expiryTime.getTime() - currentTime.getTime();
if (duration > 0) {
timer = new CountDownTimer(duration, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long diffInHours = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished - TimeUnit.HOURS.toMillis(diffInHours));
long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished - TimeUnit.HOURS.toMillis(diffInHours) - TimeUnit.MINUTES.toMillis(diffInMinutes));
if (isEndTime)
txt_product_timer.setText("Ends in " + diffInHours + ":" + String.format("%02d", diffInMinutes) + ":" + String.format("%02d", diffInSeconds));
else
txt_product_timer.setText("Deal starts in " + diffInHours + ":" + String.format("%02d", diffInMinutes) + ":" + String.format("%02d", diffInSeconds));
}
@Override
public void onFinish() {
if (isEndTime) {
// layoutRoot.setBackgroundColor(context.getResources().getColor(R.color.transparent_grey_20));
txt_product_timer.setText("Deal Ended");
shouldOpenProductPage = false;
setAlpha(true);
txt_product_timer.setTextColor(context.getResources().getColor(R.color.lybrate_red));
} else {
shouldOpenProductPage = true;
setAlpha(false);
startTimer(productInfoBean, true);
}
}
};
timer.start();
} else {
// layoutRoot.setBackgroundColor(context.getResources().getColor(R.color.transparent_grey_20));
txt_product_timer.setText("Deal Ended");
txtVw_gold_exclusive.setVisibility(View.GONE);
shouldOpenProductPage = false;
setAlpha(true);
}
}
}
}
And if I have to create a single thread for all timers than how should I do that?