热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

android特卖列表倒计时卡顿问题的解决方法

这篇文章主要为大家详细介绍了android特卖列表倒计时卡顿问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在Android的开发中,我们经常遇见倒计时的操作,通常使用Timer和Handler共同操作来完成。当然也可以使用Android系统控件CountDownTimer,这里我们封装成一个控件,也方便大家的使用。

首先上一张效果图吧:

说一下造成卡顿的原因,由于滑动的时候,adapter的getView频繁的创建和销毁,就会出现卡顿和数据错位问题,那么我们每一个item的倒计时就需要单独维护,这里我用的Handler与timer及TimerTask结合的方法,我们知道TimerTask运行在自己子线程,然后通过Timer的schedule()方法实现倒计时功能,最后通过Hander实现View的刷新,其核心代码如下:

public class CountDownView extends LinearLayout {
 
 @BindView(R.id.tv_day)
 TextView tvDay;
 @BindView(R.id.tv_hour)
 TextView tvHour;
 @BindView(R.id.tv_minute)
 TextView tvMinute;
 @BindView(R.id.tv_second)
 TextView tvSecond;
 
 @BindView(R.id.day)
 TextView day;
 @BindView(R.id.hour)
 TextView hour;
 @BindView(R.id.minute)
 TextView minute;
 
 private Context context;
 
 private int viewBg;//倒计时的背景
 private int cellBg;//每个倒计时的背景
 private int cellTextColor;//文字颜色
 private int textColor;//外部:等颜色
 private int textSize = 14;//外部文字大小
 private int cellTextSize = 12;//cell文字大小
 
 private TimerTask timerTask = null;
 private Timer timer = new Timer();
 private Handler handler = new Handler() {
 
 public void handleMessage(Message msg) {
  countDown();
 }
 };
 
 public CountDownView(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 this.cOntext= context;
 }
 
 public CountDownView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 this.cOntext= context;
 initAttrs(attrs, defStyleAttr);
 initView(context);
 }
 
 private void initAttrs(AttributeSet attrs, int defStyle) {
 TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CountDownView, defStyle,0);
 viewBg = typedArray.getColor(R.styleable.CountDownView_viewBg, Color.parseColor("#FFFFFF"));
 cellBg = typedArray.getColor(R.styleable.CountDownView_cellBg, Color.parseColor("#F4F4F4"));
 cellTextColor = typedArray.getColor(R.styleable.CountDownView_cellTextColor, Color.parseColor("#646464"));
 textColor = typedArray.getColor(R.styleable.CountDownView_TextColor, Color.parseColor("#B3B3B3"));
 textSize = (int) typedArray.getDimension(R.styleable.CountDownView_TextSize, UIUtils.dp2px(getContext(), 14));
 cellTextSize = (int) typedArray.getDimension(R.styleable.CountDownView_cellTextSize, UIUtils.dp2px(getContext(), 12));
 typedArray.recycle();
 
 }
 
 private void initView(Context context) {
 LayoutInflater inflater = (LayoutInflater) context
  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View view = inflater.inflate(R.layout.layout_countdown_layout, this);
 ButterKnife.bind(view);
 
 initProperty();
 }
 
 private void initProperty() {
 tvDay.setBackgroundColor(cellBg);
 tvHour.setBackgroundColor(cellBg);
 tvMinute.setBackgroundColor(cellBg);
 tvSecond.setBackgroundColor(cellBg);
 
 tvDay.setTextColor(cellTextColor);
 tvHour.setTextColor(cellTextColor);
 tvMinute.setTextColor(cellTextColor);
 tvSecond.setTextColor(cellTextColor);
 
 day.setTextColor(textColor);
 hour.setTextColor(textColor);
 minute.setTextColor(textColor);
 }
 
 
 public void setLeftTime(long leftTime) {
 if (leftTime <= 0) return;
 long time = leftTime / 1000;
 long day = time / (3600 * 24);
 long hours = (time - day * 3600 * 24) / 3600;
 long minutes = (time - day * 3600 * 24 - hours * 3600) / 60;
 long secOnds= time - day * 3600 * 24 - hours * 3600 - minutes * 60;
 
 setTextTime(time);
 }
 
 
 public void start() {
 if (timerTask == null) {
  timerTask = new TimerTask() {
  @Override
  public void run() {
   handler.sendEmptyMessage(0);
  }
 
  };
  timer.schedule(timerTask, 1000, 1000);
//  timer.schedule(new TimerTask() {
//  @Override
//  public void run() {
//   handler.sendEmptyMessage(0);
//  }
//  }, 0, 1000);
 }
 }
 
 public void stop() {
 if (timer != null) {
  timer.cancel();
  timer = null;
 }
 }
 
 //保证天,时,分,秒都两位显示,不足的补0
 private void setTextTime(long time) {
 String[] s = TimeUtils.formatTimer(time);
 tvDay.setText(s[0]);
 tvHour.setText(s[1]);
 tvMinute.setText(s[2]);
 tvSecond.setText(s[3]);
 }
 
 private void countDown() {
 if (isCarry4Unit(tvSecond)) {
  if (isCarry4Unit(tvMinute)) {
  if (isCarry4Unit(tvHour)) {
   if (isCarry4Unit(tvDay)) {
   stop();
   }
  }
  }
 }
 }
 
 private boolean isCarry4Unit(TextView tv) {
 int time = Integer.valueOf(tv.getText().toString());
 time = time - 1;
 if (time <0) {
  time = 59;
  tv.setText(time + "");
  return true;
 } else if (time <10) {
  tv.setText("0" + time);
  return false;
 } else {
  tv.setText(time + "");
  return false;
 }
 }
}

另一种写法:

public class CountDownTimerView extends LinearLayout {
 
 private TextView hourView, minuteView, secondView;
 private LimitTimer mTimer;
 private Handler handler;
 public static final int START_LIMIT_TIME_MSG = 0x0111;
 public static final int END_LIMIT_TIME_MSG = 0x0112;
 private long endTime, leftTime;
 private LimitTimeListener listener=null;
 
 public CountDownTimerView(Context context) {
 super(context);
 initView(context);
 }
 
 public CountDownTimerView(Context context, AttributeSet attrs) {
 super(context, attrs);
 initView(context);
 }
 
 private void initView(Context context) {
 setOrientation(HORIZONTAL);
 setGravity(Gravity.CENTER_VERTICAL);
 inflate(context, R.layout.layout_countdown_layout, this);
 hourView = (TextView) findViewById(R.id.tv_hour);
 minuteView = (TextView) findViewById(R.id.tv_minute);
 secOndView= (TextView) findViewById(R.id.tv_second);
 }
 
 public long getLeftTime() {
 return leftTime;
 }
 
 //设置剩余时间
 public void initLeftTime(long endTime) {
 endTime=endTime*1000;
 if (null != mTimer) {
  mTimer.cancel();
  mTimer = null;
 }
 this.endTime = endTime;
 mTimer = new LimitTimer(endTime, 1000);
 mTimer.start();
 if (handler != null) {
  handler.sendEmptyMessage(START_LIMIT_TIME_MSG);
 }
 }
 
 /**
 * 如果该控件使用在碎片中,返回时,则最好还是要stop
 */
 public void stopTimeCount() {
 if (null != mTimer) {
  mTimer.cancel();
  mTimer = null;
 }
 }
 
 public Handler getHandler() {
 return handler;
 }
 
 public void setHandler(Handler handler) {
 this.handler = handler;
 }
 
 private class LimitTimer extends CountDownTimer {
 
 public LimitTimer(long millisInFuture, long countDownInterval) {
  super(0 != leftTime &#63; leftTime : endTime, countDownInterval);
 }
 
 @Override
 public void onTick(long millisUntilFinished) {
  leftTime = millisUntilFinished;
  long totalSecOnd= millisUntilFinished / 1000;
  int secOnd= (int) (totalSecond % 60);
  int minute = (int) ((totalSecond / 60) % 60);
  int hour = (int) ((totalSecond / 3600) % 24);
  int day = (int) (totalSecond / (3600 * 24));
 
  formatView(hourView, hour);
  formatView(minuteView, minute);
  formatView(secondView, second);
 }
 
 @Override
 public void onFinish() {
  String zero = "00";
  hourView.setText(zero);
  minuteView.setText(zero);
  secondView.setText(zero);
  if (null != handler) {
  handler.sendEmptyMessage(END_LIMIT_TIME_MSG);
  }
  if (listener!=null){
  listener.onTimeOver(true);
  }
 }
 
 private void formatView(TextView view, int time) {
  DecimalFormat df = new DecimalFormat("#00");
  view.setText(df.format(time));
 }
 }
 
 //倒计时结束监听
 public void setOnLimitTimeListener(LimitTimeListener listener) {
 this.listener = listener;
 }
 public interface LimitTimeListener {
 void onTimeOver(boolean flag);
 }
 
 @Override
 protected void onDetachedFromWindow() {
 super.onDetachedFromWindow();
 if (handler != null) {
  handler.removeMessages(START_LIMIT_TIME_MSG);
 }
 }
}

涉及到的布局文件

<&#63;xml version="1.0" encoding="utf-8"&#63;>

 
 
 
 
 
 
 
 
 
 
 
 
 

附上源码地址:点击打开链接

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
  • flowable工作流 流程变量_信也科技工作流平台的技术实践
    1背景随着公司业务发展及内部业务流程诉求的增长,目前信息化系统不能够很好满足期望,主要体现如下:目前OA流程引擎无法满足企业特定业务流程需求,且移动端体 ... [详细]
author-avatar
松狮猫vn
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有