android 特卖列表倒计时卡顿问题

简介: 在Android的开发中,我们经常遇见倒计时的操作,通常使用Timer和Handler共同操作来完成。当然也可以使用Android系统控件CountDownTimer,这里我们封装成一个控件,也方便大家的使用。首先上一张效果图吧:说一下造成卡顿的原因,由于滑动的时候,adapter的getView频繁的创建和销毁,就会出现卡顿和数据错位问题,那么我们每一个item的倒计时就需要单独维护,

在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;
        }
    }
}
移动技术交流(Android,ios,RactNtive),请加群:278792776
附上源码地址: 点击打开链接


目录
相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
42 1
|
4月前
|
XML Java Android开发
Android Studio App开发之捕获屏幕的变更事件实战(包括竖屏与横屏切换,回到桌面与切换到任务列表)
Android Studio App开发之捕获屏幕的变更事件实战(包括竖屏与横屏切换,回到桌面与切换到任务列表)
39 0
|
4月前
|
XML Java Android开发
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
89 0
|
存储 监控 Android开发
Android卡顿优化 | ANR分析与实战(附ANR-WatchDog源码分析及实战、与AndroidPerformanceMonitor的区别)
Android卡顿优化 | ANR分析与实战(附ANR-WatchDog源码分析及实战、与AndroidPerformanceMonitor的区别)
|
4月前
|
XML Java Android开发
Android Studio App开发之列表类视图中基本适配器BaseAdapter的使用及实战(附源码 超详细)
Android Studio App开发之列表类视图中基本适配器BaseAdapter的使用及实战(附源码 超详细)
47 0
|
8月前
|
Android开发
Android 根据时间的升序或降序把数据列表进行排序
Android 根据时间的升序或降序把数据列表进行排序
92 0
|
8月前
|
Android开发
Android 短信验证码倒计时60s实现步骤
Android 短信验证码倒计时60s实现步骤
153 0
|
9月前
|
XML Java 数据处理
Android:RecyclerView封装,打造列表极简加载
此库的封装,除了刷新加载库使用了SmartRefreshLayout,其他的都是自己从0到1的开发,目前,自己已经在项目中使用,暂时没有出现任何问题,当然了,后续,也会不断的对其进行优化,增加一些其他的功能,希望有需要的小伙伴,长期关注。
236 0
|
11月前
|
Android开发
全网最优雅安卓列表项可见性检测
全网最优雅安卓列表项可见性检测
116 0
|
11月前
|
Android开发
Bug日志(四)——Android 防止多次点击(另解决多个列表子项点击)
在用户使用 应用的时候,经常会出现点击过快且多次点击同一控件的情况, 一方面这是因为应用或手机当前有些卡顿,网络卡啥的 另一方面也可能是由于很多应用并没有设置按钮点击时的 selector 或者其它按钮响应方式(例如点击按钮时按钮放大,常见于游戏),导致用户误认为没有点击到当前按钮。(比如使用selector再点击后更换背景颜色,图片等等)