首页 > 解决方案 > 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);
                }
            }
        }
    
    }

截屏

如果我必须为所有计时器创建一个线程,我应该怎么做?

标签: javaandroidmultithreadingandroid-recyclerviewcountdowntimer

解决方案


推荐阅读