我在listvew的每个项目中(在TextView中)显示一个倒数计时器,该计时器每秒钟更新一次。由于每个项目都有自己的正确计时器,因此它可以完美工作。但是每当列表视图变长并且迫使我向下滚动或向上滚动时,这些项目就会变得混乱,并且第一个隐藏的项目不显示其计时器,而是显示第一个计时器,这让我很困惑。 我知道它与listview回收站有关,这就是listview的工作方式。但是我需要解决这个问题,我不知道该怎么办。
此外,如果我删除语句if(convertView == null)
,它将得到修复。但是,当我滚动时,列表视图的加载速度将非常慢。
这是我正在使用的自定义适配器:
public class TicketAdapter extends ArrayAdapter<TicketModel> implements View.OnClickListener{
private ArrayList<TicketModel> dataSet;
Context mContext;
// View lookup cache
private class ViewHolder {
TextView txtName;
TextView txtType;
TextView txtTempsRestant;
TextView txtDate;
TextView txtSLA;
ImageView info;
RelativeLayout layout;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
long timeLeftMS = bundle.getLong("time");
int day = (int) ((timeLeftMS / (24*3600000)));
int hour = (int) ((timeLeftMS / (1000*60*60)) % 24);
int minute = (int) ((timeLeftMS / (60000)) % 60);
int second = (int)timeLeftMS % 60000 / 1000;
String timeLeftText = "";
if (day<10) timeLeftText += "0";
timeLeftText += day;
timeLeftText += ":";
if (hour<10) timeLeftText += "0";
timeLeftText += hour;
timeLeftText += ":";
if (minute<10) timeLeftText += "0";
timeLeftText += minute;
timeLeftText += ":";
if (second<10) timeLeftText += "0";
timeLeftText += seconde;
txtTempsRestant.setText(timeLeftText); //----- This is where I'm updating my textview every second ------
}
};
Handler handlerLate = new Handler(){
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
long time = bundle.getLong("time");
if (time == -1){
txtTempsRestant.setText("Ancient version");
txtTempsRestant.setTextColor(Color.parseColor("#434343"));
}
else {
txtTempsRestant.setText("Late");
txtTempsRestant.setTextColor(Color.parseColor("#434343"));
layout.setBackgroundColor(Color.parseColor("#3caa0000"));
info.setImageResource(R.drawable.haute);
}
}
};
Handler handlerFinishLate = new Handler(){
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
String Nom = bundle.getString("name");
String idTicket = bundle.getString("id");
txtTempsRestant.setText("Late");
txtTempsRestant.setTextColor(Color.parseColor("#434343"));
layout.setBackgroundColor(Color.parseColor("#3caa0000"));
info.setImageResource(R.drawable.haute);
}
};
Handler handlerAttente = new Handler(){
@Override
public void handleMessage(Message msg) {
txtTempsRestant.setText("En attente...");
txtTempsRestant.setTextColor(Color.parseColor("#434343"));
layout.setBackgroundColor(Color.parseColor("#949494"));
info.setImageResource(R.drawable.enattente);
}
};
public void startTimer(long timeLeftMS, String statut, final String Nom, final String idTicket) {
if(statut.equals("4")){
handlerAttente.sendEmptyMessage(0);
}
else{
if (timeLeftMS<0){
//handlerLate.sendEmptyMessage(0);
Bundle bundle = new Bundle();
bundle.putLong("time", timeLeftMS);
Message message = new Message();
message.setData(bundle);
handlerLate.sendMessage(message);
}
else{
CountDownTimer countDownTimer = new CountDownTimer(timeLeftMS, 1000) {
@Override
public void onTick(long l) {
Bundle bundle = new Bundle();
bundle.putLong("time", l);
bundle.putString("name", Nom);
bundle.putString("id", idTicket);
Message message = new Message();
message.setData(bundle);
handler.sendMessage(message);
}
@Override
public void onFinish() {
Bundle bundle = new Bundle();
bundle.putString("name", Nom);
bundle.putString("id", idTicket);
Message message = new Message();
message.setData(bundle);
handlerFinishLate.sendMessage(message);
}
}.start();
}
}
}
}
public TicketAdapter(ArrayList<TicketModel> data, Context context) {
super(context, R.layout.row_item_ticket, data);
this.dataSet = data;
this.mContext=context;
}
@Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
TicketModel TicketModel=(TicketModel)object;
switch (v.getId())
{
case R.id.item_info:
Snackbar.make(v, "Late? : " +TicketModel.isTicketEnRetard(), Snackbar.LENGTH_LONG)
.setAction("No action", null).show();
break;
}
}
private int lastPosition = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
TicketModel TicketModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
final ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
long timeLeft;
String Statut;
String Nom;
String idTicket;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item_ticket, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.titreTV);
viewHolder.txtDate = (TextView) convertView.findViewById(R.id.dateTV);
viewHolder.txtSLA = (TextView) convertView.findViewById(R.id.slaTV);
viewHolder.txtTempsRestant = (TextView) convertView.findViewById(R.id.SLARestantTV);
viewHolder.info = (ImageView) convertView.findViewById(R.id.item_info);
viewHolder.layout = (RelativeLayout) convertView.findViewById(R.id.backgroundRow);
timeLeft = Long.valueOf(TicketModel.getTempsRestantTicket());
Statut = TicketModel.getStatut();
Nom = TicketModel.getTitreTicket();
idTicket = TicketModel.getIdTicket();
result=convertView;
viewHolder.startTimer(timeLeft, Statut, Nom, idTicket);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.txtName = (TextView) convertView.findViewById(R.id.titreTV);
viewHolder.txtDate = (TextView) convertView.findViewById(R.id.dateTV);
viewHolder.txtSLA = (TextView) convertView.findViewById(R.id.slaTV);
viewHolder.txtTempsRestant = (TextView) convertView.findViewById(R.id.SLARestantTV);
viewHolder.info = (ImageView) convertView.findViewById(R.id.item_info);
viewHolder.layout = (RelativeLayout) convertView.findViewById(R.id.backgroundRow);
timeLeft = Long.valueOf(TicketModel.getTempsRestantTicket());
Statut = TicketModel.getStatut();
Nom = TicketModel.getTitreTicket();
idTicket = TicketModel.getIdTicket();
lastPosition = position;
viewHolder.txtName.setText(TicketModel.getTitreTicket());
viewHolder.txtDate.setText(TicketModel.getDateTicket());
viewHolder.txtSLA.setText(TicketModel.getSlaTicket());
if (Long.valueOf(TicketModel.getTempsRestantTicket())<0){
viewHolder.txtTempsRestant.setText("Late");
}
viewHolder.layout.setBackgroundColor(getColorBG(TicketModel.isTicketEnRetard()));
viewHolder.info.setOnClickListener(this);
viewHolder.info.setTag(position);
// Return the completed view to render on screen
return convertView;
}
}
答案 0 :(得分:0)
另外,如果我删除语句if(convertView == null),它将得到修复。但是,当我滚动时,列表视图的加载速度将非常慢。
ListView
致力于回收抓取视图的概念。当您向上滚动时,在第一状态下出现的视图将被抓取,并且显示在底部的新视图与之前被抓取的视图相同。
要克服此问题,该视图可以知道多少种视图:
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getCount() {
return dataSet.size();
}
@Override
public Object getItem(int position) {
return dataSet.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
也
确保您初始化。 ArrayList
为private ArrayList<TicketModel> dataSet = new ArrayList<>();
答案 1 :(得分:0)
Hey you can create list of viewHolder class and and handle each view with timer.
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
private final List<MyViewHolder> lstHolders;
private Context context;
public MyRecyclerAdapter(Context context) {
super();
this.context = context;
lstHolders = new ArrayList<>();
startUpdateTimer();
}
private Handler mHandler = new Handler();
private Runnable updateRemainingTimeRunnable = new Runnable() {
@Override
public void run() {
synchronized (lstHolders) {
long currentTime = System.currentTimeMillis();
for (MyViewHolder holder : lstHolders) {
holder.updateTimeRemaining(currentTime);
}
}
}
};
private void startUpdateTimer() {
Timer tmr = new Timer();
tmr.schedule(new TimerTask() {
@Override
public void run() {
mHandler.post(updateRemainingTimeRunnable);
}
}, 1000, 1000);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.setData(position);
synchronized (lstHolders) {
lstHolders.add(holder);
}
holder.updateTimeRemaining(System.currentTimeMillis());
}
@Override
public int getItemCount() {
return 50;
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView tvTimeRemaining;
public MyViewHolder(View itemView) {
super(itemView);
tvTimeRemaining = itemView.findViewById(R.id.txt_view);
}
public void updateTimeRemaining(long currentTime) {
String result = getData("09/21/2018 15:00:53");
String[] terms = result.split(",");
long expirationTime = getInitialTimeSecond(terms);
/* if(Integer.parseInt(terms[0])>=1){
expirationTime = 24*60*60*60*1000;
}else{
expirationTime = getInitialTimeSecond(terms);
}*/
long timeDiff = expirationTime;
if (timeDiff > 0) {
int seconds = (int) (timeDiff / 1000) % 60;
int minutes = (int) ((timeDiff / (1000 * 60)) % 60);
int hours = (int) ((timeDiff / (1000 * 60 * 60)) % 24);
Spannable hourString = new SpannableString(String.valueOf(hours));
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.RED);
hourString.setSpan(backgroundSpan, 0, hourString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvTimeRemaining.setText(hourString + " : " + minutes + " : " + seconds + "");
} else {
tvTimeRemaining.setText("Expired!!");
}
}
public void setData(int position) {
updateTimeRemaining(System.currentTimeMillis());
}
}
private long getInitialTimeSecond(String[] terms) {
long mInitialTime = DateUtils.DAY_IN_MILLIS * Integer.parseInt(terms[0]) +
DateUtils.HOUR_IN_MILLIS * Integer.parseInt(terms[1]) +
DateUtils.MINUTE_IN_MILLIS * Integer.parseInt(terms[2]) +
DateUtils.SECOND_IN_MILLIS * Integer.parseInt(terms[3]);
return mInitialTime;
}
private String getData(String dateStop) {
//Calendar c = Calendar.getInstance();
//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d1 = Calendar.getInstance().getTime();
Date d2 = null;
String result = null;
try {
// d1 = format.parse(c.getTime().toString());
d2 = format.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
result = diffDays + "," + diffHours + "," + diffMinutes + "," + diffSeconds;
System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
if you have any doubt,Let me know.