@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_move_list, container, false); mRecycler = v.findViewById(R.id.id_move_list_recycler); setAdapter(); handler.postDelayed(runnable,1000); return v; }
Runnable runnable = new Runnable() { @Override public void run() { recLen++; mAdapter.notifyDataSetChanged(); // 每秒执行一次 handler.postDelayed(this,1000); } };
/** * 计算两个时间的时间差 * @param startTime * @param endTime * @param format 格式 * @return 返回集合,分别以 时 分 秒 储存集合中 */ public static ArrayList<Long> dateDiff(String startTime, String endTime, String format) { ArrayList<Long> longs = new ArrayList<>(); // 按照传入的格式生成一个simpledateformate对象 SimpleDateFormat sd = new SimpleDateFormat(format); long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数 long nh = 1000 * 60 * 60;// 一小时的毫秒数 long nm = 1000 * 60;// 一分钟的毫秒数 long ns = 1000;// 一秒钟的毫秒数 long diff; long day = 0; long hour = 0; long min = 0; long sec = 0; // 获得两个时间的毫秒时间差异 try { diff = sd.parse(startTime).getTime() - sd.parse(endTime).getTime(); day = diff / nd;// 计算差多少天 hour = diff % nd / nh + day * 24;// 计算差多少小时 min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟 sec = diff % nd % nh % nm / ns;// 计算差多少秒 // 输出结果 // System.out.print("返回的时间:" +startTime); // System.out.print("当前时间:" +endTime); System.out.println("时间相差:" + day + "天" + (hour - day * 24) + "小时" + (min - day * 24 * 60) + "分钟" + sec + "秒。"); longs.add(hour); longs.add(min); longs.add(sec); return longs; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } longs.add(hour); longs.add(min); longs.add(sec); return longs; }
// 用于解决时差8小时问题 public static String formDate(Date value) { String newValue = "null"; try { SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (value == null) { return "null"; } Calendar ca = Calendar.getInstance(); ca.setTime(value); // 用于解决时差8小时问题 //ca.add(Calendar.HOUR_OF_DAY, 8); newValue = f.format(ca.getTime()); } catch (Exception e) { e.printStackTrace(); } return newValue; }
adpater:
ArrayList<Long> timeItem = TimeConversionUtil.dateDiff(
// 服务器返回时间 TimeConversionUtil.formDate(mDatas.getTargetCompletionAt()), // 本地时间 TimeConversionUtil.getTime1(), // 指定格式 "yyyy-MM-dd HH:mm:ss"); // 不是负数 if (timeItem.get(0).longValue() >= 0) { // 小于 10 分钟 if (timeItem.get(1).longValue() < 10 && // 且不为负数 timeItem.get(1).longValue() >= 0 && // 且 秒大于 0 timeItem.get(2).longValue() >= 0) { String showM = ""; String showS = ""; // 分钟为个位 补 0 if (timeItem.get(1).longValue() < 10) { showM = "0" + timeItem.get(1).longValue(); } else { showM = String.valueOf(timeItem.get(1).longValue()); } // 秒为个位 补 0 if (timeItem.get(2).longValue() < 10) { showS = "0" + timeItem.get(2).longValue(); } else { showS = String.valueOf(timeItem.get(2).longValue()); } holder.setText(R.id.item_move_pick_countdown_time, showM + ":" + showS); } else { // 超时 holder.setText(R.id.item_move_pick_countdown_time, "TimeOut"); } } else { // 超时 holder.setText(R.id.item_move_pick_countdown_time, "TimeOut"); } }