我有一个listView
,其中每个项目都应显示倒计时,事件日期来自Firebase。我的倒数计时器正在工作,但前提是该列表中只有一项。
在我的片段中,我有一个listview
。
在我的adapter_events
中,我有一个文本字段,希望在其中显示每个项目的计数器(textViewTime)
EventsAdapter.java :
public class EventsAdapter extends BaseAdapter {
private Context mContext;
private TextView NomeDoEvento;
private TextView TempoRestante;
private TextView DataDoEvento;
//private TextView DataLocal;
public EventsAdapter(Context context) {
this.mContext = context;
}
public int getCount() {
return (SharedResources.getInstance().getEventos().size());
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.adapter_events, null);
NomeDoEvento = v.findViewById(R.id.textViewNomeDoEvento);
TempoRestante = v.findViewById(R.id.textViewTempo);
DataDoEvento = v.findViewById(R.id.textViewDataLocal);
// Hora Atual
long currentTimeMillis = Calendar.getInstance().getTimeInMillis();
//Dados do evento
Evento evento = SharedResources.getInstance().getEventos().get(position);
NomeDoEvento.setText(evento.getNomeDoEvento());
//Data do evento
String[] data = evento.getDataWorld().split("/");
GregorianCalendar HorarioEvento = new GregorianCalendar(Integer.parseInt(data[0]) ,Integer.parseInt(data[1])-1,
Integer.parseInt(data[2]),Integer.parseInt(data[3]),Integer.parseInt(data[4]),0);
long expiryTime = HorarioEvento.getTimeInMillis() - currentTimeMillis;
DataDoEvento.setText(HorarioEvento.getTime().toString());
TempoRestante.setText("");
new CountDownTimer(expiryTime, 1000) {
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished / 1000; // reminder: 1 sec = 1000 millis
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
String dayFormat = "d ";
String hoursFormat = "h ";
String minutesFormat = "m ";
String secondFormat = "s";
String time = days + dayFormat + hours % 24 + hoursFormat + minutes%60 + minutesFormat + seconds % 60 +secondFormat;
TempoRestante.setText(time);
}
// Finished: counted down to 0
public void onFinish() {
TempoRestante.setText("Evento iniciado!");
}
}.start();
return v;
}
}
Fragment.java :
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_events, container, false);
ListEventos = v.findViewById(R.id.ListView_Eventos);
Progress = v.findViewById(R.id.progressBar);
Progress.setVisibility(View.INVISIBLE);
Progress.setVisibility(View.VISIBLE);
DatabaseCommunication.RecuperaEventos(v.getContext());
return v;
}
答案 0 :(得分:0)
每次需要显示新视图时,您都在创建一个倒数计时器。请注意,例如,列表视图只是回收视图。如果只有10个视图适合屏幕显示,即使您有100个项目,也只会创建10个视图,并且您的项目数据将仅绑定到该视图。
还请注意,您在TempoRestante
内抓住CountdownTimer
会造成泄漏。
P.S。 请正确命名变量,并使用驼峰式大小写。