有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

亲爱的

我想通过在textview中显示值来测试在RecyerView中实现倒计时。倒计时在一个单独的类中。该值实际上显示在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类,它工作得很好


共 (1) 个答案