我正在研究显示拍卖对象的recyclerview。
每个对象的结束时间都不同,我想显示从当前日期/时间到拍卖结束日期/时间的倒数。
使用我的代码实现,每个对象都显示正确的倒计时时间,但是值显示闪烁,这不是一个干净的视图。
我认为问题是由于对所有对象使用相同的处理程序。
这是我当前的代码:
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.txtreferencia.setText(subastas.get(position).getreferencia());
holder.txttitulo.setText(subastas.get(position).gettitulo());
holder.txtdescripcion.setText(subastas.get(position).getdescripcion());
String marca = subastas.get(position).getmarca();
String modelo = subastas.get(position).getmodelo();
String ano = subastas.get(position).getano();
String generacion = subastas.get(position).getgeneracion();
String serie = subastas.get(position).getserie();
String motor = subastas.get(position).getmotor();
String num_pujas = subastas.get(position).getnum_pujas();
String mejor_puja = subastas.get(position).getmejor_puja();
final String termina = subastas.get(position).gettermina();
Log.d("TERMINA","TERMINA>"+termina);
if (mejor_puja == null || mejor_puja.isEmpty() || mejor_puja.equals("null")){
holder.txtmejor_puja.setVisibility(View.GONE);
}
mejor_puja = mejor_puja.replace('.',',');
holder.txtmarca.setText(marca+ " "+modelo+", "+ano+", "+generacion+", "+serie+" "+motor);
holder.txtnum_pujas.setText(num_pujas+" pujas");
holder.txtmejor_puja.setText("Mejor precio: "+mejor_puja+" €");
String url_foto = "https://.../"+ subastas.get(position).getfoto();
Glide.with(context).load(url_foto).into(holder.imageView);
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");
// Please here set your event date//YYYY-MM-DD
Date futureDate = dateFormat.parse(termina);
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);
Log.d("TERMINA","TERMINA>"+days);
long hours = diff / (60 * 60 * 1000);
diff -= hours * (60 * 60 * 1000);
Log.d("TERMINA","TERMINA>"+hours);
long minutes = diff / (60 * 1000);
Log.d("TERMINA","TERMINA>"+minutes);
diff -= minutes * (60 * 1000);
long seconds = diff / 1000;
Log.d("TERMINA","TERMINA>"+seconds);
String dias = String.valueOf(days);
String minutos = String.valueOf(minutes);
String horas = String.valueOf(hours);
String segundos = String.valueOf(seconds);
Log.d("TERMINA","TERMINA>current:"+currentDate);
Log.d("TERMINA","TERMINA>future:"+futureDate);
holder.txtRestante.setText(dias+"d"+horas+"h"+minutos+"m"+segundos+"s");
} else {
holder.txtRestante.setText("Subasta terminada");
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 1 * 1000);
}
如何对每个对象进行清零计数?