recyclerview中的倒数计时器无法正常运作 [英] Countdown timer in recyclerview not working properly

查看:107
本文介绍了recyclerview中的倒数计时器无法正常运作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还创建了一个recyclerview,其中也包含多个数据和一个倒数计时器.直到我无法滚动列表,所有的东西才能正常工作.当我向下滚动列表时,countdowntimer将与以上视图项冲突.它计算当前的观看时间和随机时间.我尝试了太多的方法来解决它,但是不能解决.请帮帮我.提前致谢 .

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 .

这是我的RecyclerAdapter的代码.

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

推荐答案

问题是,在每个onBindViewHolder调用中,您都在创建一个新的CountDownTimer并更新该ViewHolder.因此,经过几次滚动后,多个CountDownTimer将尝试更新相同的ViewHolder.

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.

每个ViewHolder仅创建一个CountDownTimer.为此,请在ViewHolder上设置新的CountDownTimer之前,通过调用.cancel();取消CountDownTimer.

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.

为了访问已在ViewHolder上设置的CountDownTimer,请在onCreateViewHolder上创建它,并将其作为字段放在MyViewHolder内.

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.

MyViewHolder上有一个名为say update的公共方法,该方法完成您在onBindViewHolder中编写的所有过程,并在适配器的onBindViewHolder中进行简单的调用更新. (也更干净的代码:))

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 :) )

这篇关于recyclerview中的倒数计时器无法正常运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆