java - RecyclerView中的多个倒数计时器滞后
问题描述
我android.os.CountDownTimer
在回收站视图的每一行中使用,但是当我在回收站视图中向上/向下滚动时,我的应用程序滞后。我应该在单独的线程中运行计时器吗?如果方法正确,那么我应该为计时器创建一个单独的线程还是为每个计时器创建一个线程?
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);
}
}
}
}
如果我必须为所有计时器创建一个线程,我应该怎么做?
解决方案
推荐阅读
- javascript - js对象更新嵌套对象
- javascript - 寻找具有隐式返回的函数对象——类似于箭头函数
- z3 - 无法变异的 Z3 数组
- regex - 如何拒绝所有非拉丁字母和数字,但允许使用正则表达式的拉丁数字和字母和特殊字符?
- c - 混合 .wav 文件
- r - 使用 if 语句过滤最小/最大值 R
- mongodb - 使用 MongoDB 更新数组并在同一数组数组中推送对象
- r - nls() 错误 - 收敛失败:奇异收敛 (7)
- r - 如何在 R 数据框中生成一个新列,其中包含来自多列的有序项
- tensorflow - 试图将 lasagne.layers.SliceLayer(input_from_previous_layer, -1, 1) fom theano 转换为 keras