gpt4 book ai didi

java - RecyclerView 中的 Android CountDownTimer

转载 作者:行者123 更新时间:2023-12-02 04:22:43 24 4
gpt4 key购买 nike

我通过在 textview 中显示值来在 ReclyerView 中插入 countDownTimercountDownTimer 在运行我的 Activity 时工作正常我的问题是,当我滚动 ReclyerView 并返回到之前的项目时,计时器无法正常工作并显示不正确的数字

onBindViewHolder

if (mListDiscountTab.get(holder.getAdapterPosition()).getmCountDownTimer() == null) {
mListDiscountTab.get(holder.getAdapterPosition()).setmCountDownTimer(new CountDownTimer((((mListDiscountTab.get(holder.getAdapterPosition()).getHour() * 60 * 60) + (mListDiscountTab.get(holder.getAdapterPosition()).getMinute() * 60) + (mListDiscountTab.get(holder.getAdapterPosition()).getSecond())) * 1000), 1000) {
@Override
public void onTick(long millisUntilFinished) {
Log.d("herePOU", holder.getAdapterPosition()+"");
long sec = (millisUntilFinished / 1000);
((ViewHolderItem) holder).tv_item_timer_hour_home_discount.setText(NumberConverter.Number_English_to_Persian(sec / 3600));
((ViewHolderItem) holder).tv_item_timer_minute_home_discount.setText(NumberConverter.Number_English_to_Persian(((sec % 3600) / 60)));
((ViewHolderItem) holder).tv_item_timer_second_home_discount.setText(NumberConverter.Number_English_to_Persian((sec % 3600) % 60));
}

@Override
public void onFinish() {
mListDiscountTab.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
}
});
mListDiscountTab.get(holder.getAdapterPosition()).getmCountDownTimer().start();

}

最佳答案

请尝试向您的对象添加字段 sec,通过检查对象的 sec 来设置持有者的 textView UI。

onBindViewHolder

if (mListDiscountTab.get(holder.getAdapterPosition()).getmCountDownTimer() == null) {
mListDiscountTab.get(holder.getAdapterPosition()).setmCountDownTimer(new CountDownTimer((((mListDiscountTab.get(holder.getAdapterPosition()).getHour() * 60 * 60) + (mListDiscountTab.get(holder.getAdapterPosition()).getMinute() * 60) + (mListDiscountTab.get(holder.getAdapterPosition()).getSecond())) * 1000), 1000) {
@Override
public void onTick(long millisUntilFinished) {
Log.d("herePOU", holder.getAdapterPosition()+"");
long sec = (millisUntilFinished / 1000);
mListDiscountTab.get(holder.getAdapterPosition()).setSec(sec);
notifyDataChanged();
}

@Override
public void onFinish() {
mListDiscountTab.remove(holder.getAdapterPosition());
notifyDataChanged();
}
});
mListDiscountTab.get(holder.getAdapterPosition()).getmCountDownTimer().start();
}

((ViewHolderItem) holder).tv_item_timer_hour_home_discount.setText(NumberConverter.Number_English_to_Persian(mListDiscountTab.get(holder.getAdapterPosition()).getSec() / 3600));
((ViewHolderItem) holder).tv_item_timer_minute_home_discount.setText(NumberConverter.Number_English_to_Persian(((mListDiscountTab.get(holder.getAdapterPosition()).getSec() % 3600) / 60)));
((ViewHolderItem) holder).tv_item_timer_second_home_discount.setText(NumberConverter.Number_English_to_Persian((mListDiscountTab.get(holder.getAdapterPosition()).getSec() % 3600) % 60));

关于java - RecyclerView 中的 Android CountDownTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56626205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com