简体   繁体   English

recyclerview中的倒计时器无法正常工作

[英]Countdown timer in recyclerview not working properly

I have created a recyclerview that contains multiple data and a countdown timer too . 我创建了一个包含多个数据和倒数计时器的recyclerview。 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 . 当我向下滚动列表时,countdowntimer将与上面的视图项冲突。 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 . 这是我的RecyclerAdapter的代码。

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

The issue is that on every onBindViewHolder call you are creating a new CountDownTimer and updating that ViewHolder . 问题是,在每个onBindViewHolder调用中,您正在创建一个新的CountDownTimer并更新该ViewHolder So after a couple of scrolls, multiple CountDownTimer will try to update the same ViewHolder . 因此,在几次滚动之后,多个CountDownTimer将尝试更新相同的ViewHolder

Create only one CountDownTimer per ViewHolder . 每个ViewHolder只创建一个CountDownTimer To do that, cancel the CountDownTimer by calling the .cancel(); 为此,请通过调用.cancel();取消CountDownTimer .cancel(); on it before setting a new CountDownTimer on the ViewHolder . ViewHolder上设置新的CountDownTimer之前。

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. 要访问已在ViewHolder上设置的CountDownTimer ,请在onCreateViewHolder上创建它,并将其作为字段放在MyViewHolder中。

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. MyViewHolder上有一个名为say update的公共方法,它执行你在onBindViewHolder编写的所有程序,并在适配器的onBindViewHolder进行简单的调用更新。 (cleaner code too :) ) (清洁代码也:))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM