Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Countdown timer in recyclerview not working properly

I have created a recyclerview that contains multiple data and a countdown timer too . All the things working properly untill i couldn't scroll the list . when i scroll down the list countdowntimer will conflicts with the above view items . It counts the current view time and the random time also . I tried too many things to solved it but couldn't able to . Please help me out . Thanks in advance .

Here is my RecyclerAdapter's code .

public class ReferendumQuestionAdapter extends RecyclerView.Adapter<ReferendumQuestionAdapter.MyViewHolder> {

List<QuestionsModel.QuestionsData> questionBeen = new ArrayList<>();
List<QuestionsModel.LikedImages> likedImagesList = new ArrayList<>();
Activity activity;
boolean channerlView = false;
OnViewClick onViewClick ;

AppPreference preference;
CustomDialog customDialog;
AdminAPI adminAPI;


ReferendumImagesAdapter imagesRecycleViewAdapter;
List<CommentModel.GetCommentData> commentDataList = new ArrayList<>();
CommentViewAdapter commentViewAdapter;

public ReferendumQuestionAdapter(List<QuestionsModel.QuestionsData> questionBeen, Activity activity, boolean channerlView , OnViewClick onViewClick) {
    this.questionBeen = questionBeen;
    this.activity = activity;
    this.channerlView = channerlView;
    this.onViewClick = onViewClick ;
    this.preference = new AppPreference(activity);
    this.customDialog = new CustomDialog(activity);
    this.adminAPI = ServiceGenerator.getAPIClass();
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.layout_referendum_question, parent, false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {

    holder.txtQuestion.setText(questionBeen.get(position).getRef_question());
    holder.txt_channelName.setText(questionBeen.get(position).getPost_by());
    holder.txt_userName.setText(questionBeen.get(position).getNews_channel_name());

    holder.txt_views.setText(questionBeen.get(position).getTotal_view() + " View  ");
    holder.txt_comments.setText(questionBeen.get(position).getTotal_comment() + " Comment");
    holder.txt_referendums.setText(questionBeen.get(position).getTotal_like() + " Referendum ");


    String getUserName = holder.txt_userName.getText().toString().trim();
    if(getUserName==null || getUserName.isEmpty()){
        holder.txt_userName.setVisibility(View.GONE);
    }


    String channelImageURL = questionBeen.get(position).getPost_user_image();
    Glide.with(activity)
            .load(ApiURLs.IMAGE_URL + channelImageURL)
            .error(R.drawable.noimagefound)
            .into(holder.imgChannel);


    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    String startDateTime = questionBeen.get(position).getCurrent_date_time();
    String endDateTime = questionBeen.get(position).getEnd_date_time();
    Date startDate = null, endDate = null;
    try {
        startDate = simpleDateFormat.parse(startDateTime);
        endDate = simpleDateFormat.parse(endDateTime);

    } catch (ParseException e) {
        e.printStackTrace();
    }

    long start_millis = startDate.getTime();
    long end_millis = endDate.getTime();
    long total_millis = (end_millis - start_millis);

    Log.e("checkTime" , "-->" + total_millis);

    CountDownTimer cdt = new CountDownTimer(total_millis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            long days = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
            millisUntilFinished -= TimeUnit.DAYS.toMillis(days);

            long hours = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
            millisUntilFinished -= TimeUnit.HOURS.toMillis(hours);

            long minutes = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
            millisUntilFinished -= TimeUnit.MINUTES.toMillis(minutes);

            long seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);

            holder.txtTime.setText(" " + days + " D: " + hours + " H: " + minutes + " M: " + seconds + " S  ");
        }

        @Override
        public void onFinish() {
            holder.txtTime.setText("Finish!");
            onViewClick.onClick(position);
        }
    };
    cdt.start();
}
@Override
public int getItemCount() {
    return questionBeen.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {

    RecyclerView questionRecycle;
    TextView txtQuestion, txtTime, txt_content, txt_userName, txt_totalComment, txt_submitComment, txt_channelName , txt_views , txt_referendums  ,txt_comments;
    ImageView imgShare, imgChannel;
    EditText edt_comment;

    public MyViewHolder(View itemView) {
        super(itemView);
        questionRecycle = (RecyclerView) itemView.findViewById(R.id.recyclerview_images);
        txtQuestion = (TextView) itemView.findViewById(R.id.txt_question);
        txtTime = (TextView) itemView.findViewById(R.id.txt_time);
        txt_content = (TextView) itemView.findViewById(R.id.txt_content);
        txt_userName = (TextView) itemView.findViewById(R.id.txt_userName);
        txt_totalComment = (TextView) itemView.findViewById(R.id.txt_totalComment);
        txt_submitComment = (TextView) itemView.findViewById(R.id.txt_submitComment);
        txt_channelName = (TextView) itemView.findViewById(R.id.txt_channelName);
        txt_views = (TextView) itemView.findViewById(R.id.txt_views);
        txt_referendums = (TextView) itemView.findViewById(R.id.txt_referendums);
        txt_comments = (TextView) itemView.findViewById(R.id.txt_comments);
        imgShare = (ImageView) itemView.findViewById(R.id.img_share);
        imgChannel = (ImageView) itemView.findViewById(R.id.img_channel);
        edt_comment = (EditText) itemView.findViewById(R.id.edt_comment);

    }
}

public interface OnViewClick {
    void onClick(int position);
}
like image 862
Keval Shukla Avatar asked Dec 06 '17 05:12

Keval Shukla


1 Answers

The issue is that on every onBindViewHolder call you are creating a new CountDownTimer and updating that ViewHolder. So after a couple of scrolls, multiple CountDownTimer will try to update the same ViewHolder.

Create only one CountDownTimer per ViewHolder. To do that, cancel the CountDownTimer by calling the .cancel(); on it before setting a new CountDownTimer on the ViewHolder.

In order to get access to the CountDownTimer already set on a ViewHolder, create it on onCreateViewHolder and put it inside MyViewHolder as a field.

Have a public method on MyViewHolder called say update which does all the procedure you have written in onBindViewHolder and simple call update in onBindViewHolder of the Adapter. (cleaner code too :) )

like image 152
vader Avatar answered Nov 14 '22 22:11

vader