Multiple count down timers in RecyclerView flickering when scrolled

前端 未结 3 1410
孤街浪徒
孤街浪徒 2020-11-30 09:27

I have implemented count down timer for each item of RecyclerView which is in a fragment activity. The count down timer shows the time remaining for expiry. The count down t

3条回答
  •  -上瘾入骨i
    2020-11-30 09:53

    As Andrei Lupsa said you should hold CountDownTimer reference in your ViewHolder, if you didn't want to timer reset when scrolling (onBindViewHolder) , you should check if CountDownTimer reference is null or not in onBindViewHolder :

    public void onBindViewHolder(final FeedViewHolder holder, final int position) {
    
          ...
    
          if (holder.timer == null) {
              holder.timer = new CountDownTimer(expiryTime, 500) {
    
              ...
    
              }.start();   
          }
    }
    
    
    public static class FeedViewHolder extends RecyclerView.ViewHolder {
    
           ...
    
           CountDownTimer timer;
    
           public FeedViewHolder(View itemView) {
    
              .... 
      }
    }
    

提交回复
热议问题