gpt4 book ai didi

java - RecyclerView中的Android倒计时器在两个值之间闪烁

转载 作者:行者123 更新时间:2023-12-02 01:53:47 24 4
gpt4 key购买 nike

亲爱的,

我想通过在 TextView 中显示值来测试在 ReclyerView 中实现 countDownTimer。 countDownTimer 在一个单独的类中。该值实际上显示在 TextView 中,但它在两个值之间闪烁,其中一个是正确的,但另一个我不知道它来自哪里。请查看下面的代码并让我知道我缺少什么?

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 类

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

请注意,我已经在没有使用 RecyclerView 的情况下测试了 myTimer 类,并且它工作得很好。

最佳答案

检查这个Multiple count down timers in RecyclerView flickering when scrolled

在 onBindViewHolder 上检查计时器是否已初始化。如果初始化则先取消再启动。

关于java - RecyclerView中的Android倒计时器在两个值之间闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52596104/

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