首页 技术 正文
技术 2022年11月18日
0 收藏 800 点赞 3,418 浏览 3785 个字
  1. public class MainActivity extends Activity {
  2. /**截至时间数据源**/
  3. private List<Date> listData;
  4. /**当前时间**/
  5. private long time_Current;
  6. /**ListView控件**/
  7. private ListView listView;
  8. /**适配器**/
  9. private MyCountAdapter myCountAdapter;
  10. //这里很重要,使用Handler的延时效果,每隔一秒刷新一下适配器,以此产生倒计时效果
  11. private Handler handler_timeCurrent = new Handler(){
  12. @Override
  13. public void handleMessage(Message msg) {
  14. time_Current = time_Current+1000;
  15. myCountAdapter.notifyDataSetChanged();
  16. handler_timeCurrent.sendEmptyMessageDelayed(0,1000);
  17. }
  18. };
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. listView = (ListView) findViewById(R.id.listView);
  24. //模拟活动截至时间数据
  25. listData = new ArrayList<Date>();
  26. listData.add(new Date(2016,3,16,8,20,31));
  27. listData.add(new Date(2016,3,16,8,21,20));
  28. listData.add(new Date(2016,3,16,13,21,22));
  29. listData.add(new Date(2016,3,16,8,21,20));
  30. listData.add(new Date(2016,3,16,8,21,23));
  31. listData.add(new Date(2016,3,16,14,21,20));
  32. listData.add(new Date(2016,3,16,8,21,23));
  33. listData.add(new Date(2016,3,16,8,21,24));
  34. listData.add(new Date(2016,3,16,8,21,20));
  35. listData.add(new Date(2016,3,16,8,22,25));
  36. listData.add(new Date(2016,3,16,8,23,20));
  37. listData.add(new Date(2016,3,16,8,24,26));
  38. listData.add(new Date(2016,3,16,8,25,20));
  39. listData.add(new Date(2016,3,16,8,24,25));
  40. listData.add(new Date(2016,3,16,8,25,20));
  41. listData.add(new Date(2016,3,16,8,24,26));
  42. listData.add(new Date(2016,3,16,11,20,20));
  43. listData.add(new Date(2016,3,16,14,40,20));
  44. listData.add(new Date(2016,3,16,8,44,20));
  45. listData.add(new Date(2016,3,16,10,20,20));
  46. //模拟当前服务器时间数据
  47. Date date = new Date(2016,3,16,8,20,20);
  48. time_Current = date.getTime();
  49. myCountAdapter = new MyCountAdapter();
  50. listView.setAdapter(myCountAdapter);
  51. handler_timeCurrent.sendEmptyMessageDelayed(0,1000);
  52. }
  53. //防止当前Activity结束以后,   handler依然继续循环浪费资源
  54. @Override
  55. protected void onDestroy() {
  56. handler_timeCurrent.removeCallbacksAndMessages(null);
  57. super.onDestroy();
  58. }
  59. public class MyCountAdapter extends BaseAdapter{
  60. @Override
  61. public int getCount() {
  62. return listData.size();
  63. }
  64. @Override
  65. public Object getItem(int position) {
  66. return listData.get(position);
  67. }
  68. @Override
  69. public long getItemId(int position) {
  70. return position;
  71. }
  72. @Override
  73. public View getView(int position, View convertView, ViewGroup parent) {
  74. ViewHolder holder = null;
  75. if(convertView == null){
  76. convertView = View.inflate(MainActivity.this, R.layout.item_adapter_listview, null);
  77. holder = new ViewHolder();
  78. holder.tv_hour = (TextView) convertView.findViewById(R.id.tv_hour);
  79. holder.tv_minute = (TextView) convertView.findViewById(R.id.tv_minute);
  80. holder.tv_second = (TextView) convertView.findViewById(R.id.tv_second);
  81. convertView.setTag(holder);
  82. }else{
  83. holder = (ViewHolder) convertView.getTag();
  84. }
  85. Date date_finish = listData.get(position);
  86. updateTextView( date_finish.getTime()-time_Current, holder);
  87. return convertView;
  88. }
  89. /****
  90. * 刷新倒计时控件
  91. */
  92. public void updateTextView(long times_remain,ViewHolder hoder) {
  93. if (times_remain <= 0) {
  94. hoder.tv_hour.setText(“00”);
  95. hoder.tv_minute.setText(“00”);
  96. hoder.tv_second.setText(“00”);
  97. return;
  98. }
  99. //秒钟
  100. long time_second = (times_remain/1000)%60;
  101. String str_second;
  102. if (time_second < 10) {
  103. str_second = “0” + time_second;
  104. } else {
  105. str_second = “” + time_second;
  106. }
  107. long time_temp = ((times_remain / 1000) – time_second) / 60;
  108. //分钟
  109. long time_minute = time_temp % 60;
  110. String str_minute;
  111. if (time_minute < 10) {
  112. str_minute = “0” + time_minute;
  113. } else {
  114. str_minute = “” + time_minute;
  115. }
  116. time_temp = (time_temp – time_minute) / 60;
  117. //小时
  118. long time_hour = time_temp;
  119. String str_hour;
  120. if (time_hour < 10) {
  121. str_hour = “0” + time_hour;
  122. } else {
  123. str_hour = “” + time_hour;
  124. }
  125. hoder.tv_hour.setText(str_hour);
  126. hoder.tv_minute.setText(str_minute);
  127. hoder.tv_second.setText(str_second);
  128. }
  129. private class ViewHolder{
  130. /** 小时 **/
  131. private TextView tv_hour;
  132. /** 分钟 **/
  133. private TextView tv_minute;
  134. /** 秒 **/
  135. private TextView tv_second;
  136. }
  137. }
  138. }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848