0

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);
                }
            }
        }
    
    }

Screenshot

And if I have to create a single thread for all timers than how should I do that?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mirza Asad
  • 606
  • 1
  • 7
  • 18
  • Does this answer your question? [Multiple count down timers in RecyclerView flickering when scrolled](https://stackoverflow.com/questions/38890863/multiple-count-down-timers-in-recyclerview-flickering-when-scrolled) – ADM Mar 18 '21 at 09:25
  • It will be better if you Use a Single `CountDownTimer` in Adapter and use notify method variant with payload . – ADM Mar 18 '21 at 09:27
  • @ADM no that does not answer my question – Mirza Asad Mar 18 '21 at 09:34
  • 1
    Consider not using alpha for timed out products because it's [expensive to render](https://developer.android.com/topic/performance/rendering/overdraw#rt). You can use half-transparent overlay instead which will give you similar effect but much cheaper. – Pawel Mar 18 '21 at 17:24
  • @Pawel setAlpha() method is already doing what you told (changing visibility of half transparent overlay when product is timed out) – Mirza Asad Mar 19 '21 at 06:25

0 Answers0