Aboodnet
Aboodnet

Reputation: 143

Android Count Down Timer in RecyclerView flickers between two values

Dears,

I wanted to test implementing a countDownTimer inside ReclyerView by showing the value inside a textview. The countDownTimer is in a separate class. The value actually shows in the textview but it is flicking between two values which one of them is correct but the other one i have no idea where it came from. Please take a look at my code below and let me know what am i missing?

public class MatchesAdapter extends 
RecyclerView.Adapter<MatchesAdapter.myViewHolder> {

Context con;
private ArrayList<Matches> matchesAdapterList;


// Constructor
public MatchesAdapter(Context con, ArrayList<Matches> List) {
    matchesAdapterList = List;
    this.con = con;
}


public static class myViewHolder extends RecyclerView.ViewHolder{
    public TextView tvTimer;
    ConstraintLayout ccc;

    public myViewHolder(@NonNull View itemView) {
        super(itemView);
        tvTimer = itemView.findViewById(R.id.tvTimer);
        ccc = itemView.findViewById(R.id.cardViewContainer);
    }
}

@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_item_layout, parent, false);
    myViewHolder vh = new myViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {

       final Matches currentItem = matchesAdapterList.get(position);

   // This is where i am struggling
       myTimer t = new myTimer(con, holder.tvTimer, currentItem.getMatchDate());
       t.countDownStart();
}

@Override
public int getItemCount() {
    return matchesAdapterList.size();
}
}

MyTimer class

public class myTimer {

Context con;
TextView tvTimer;
Handler handler;
Runnable runnable;
String match_date_time;

public myTimer(Context con, TextView tvTimer, String match_date_time) {
        this.con = con;
        this.tvTimer = tvTimer;
        this.match_date_time = match_date_time;
}


public void countDownStart() {
    handler = new Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, 1000);
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+3:00"));
                Date futureDate = dateFormat.parse(match_date_time); // provided value is something like this "2018-10-15 21:00:00"
                Date currentDate = new Date();
                if (!currentDate.after(futureDate)) {
                    long diff = futureDate.getTime() - currentDate.getTime();
                    long days = diff / (24 * 60 * 60 * 1000);
                    diff -= days * (24 * 60 * 60 * 1000);
                    long hours = diff / (60 * 60 * 1000);
                    diff -= hours * (60 * 60 * 1000);
                    long minutes = diff / (60 * 1000);
                    diff -= minutes * (60 * 1000);
                    long seconds = diff / 1000;
                    tvTimer.setText(String.format("%02d", days) + " " + String.format("%02d", hours) + " " + String.format("%02d", minutes) + " " + String.format("%02d", seconds) );
                    if (days < 1) {
                        tvTimer.setTextColor(con.getResources().getColor(R.color.RedError));
                    }
                } else {
                    tvTimer.setText("Your time is up");
                }
            } catch (Exception e) {
                System.out.println(e.getLocalizedMessage().toString());
            }
        }
    };
    handler.postDelayed(runnable, 1000);
}
}

Please note that i have tested myTimer class without using RecyclerView and it is working just fine.

Upvotes: 0

Views: 2377

Answers (1)

Ami Trambadia
Ami Trambadia

Reputation: 101

check this Multiple count down timers in RecyclerView flickering when scrolled

On onBindViewHolder check if timer is already initialized or not. If initialized then cancel it first and then start it.

Upvotes: 2

Related Questions