关于listview中添加倒计时功能卡顿问题

项目需要在listview中的每个item上加一个倒计时功能。listview就是普通的listview,倒计时也是用的android自带的倒计时功能,末尾会附带倒计时的显示最简单的textview自定义组件。本篇文章最主要的还是来给自己作个记录长个知识。如果listview的父布局写成wrap_content的话,这个listview的adapter的getview方法会不停地执行,其实是在计算子布局的高度。问题困扰了几天,最后就只改了一个属性就解决卡顿问题。知道这个知识点的就不要来喷了,也希望可以帮助 不知道这个的童鞋们

import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2016-06-23.
 */
public class CountDownTimerView extends LinearLayout {
    private TextView mCountdownTimer;
    private CountDownTimer timer;

    public CountDownTimerView(Context context) {
        super(context);
    }

    public CountDownTimerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_countdowntimer, this);
        mCountdownTimer = (TextView) findViewById(R.id.countdown_timer);
//        mCountdownTimer = new TextView(context);
    }

    public CountDownTimerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_countdowntimer, this);
        mCountdownTimer = (TextView) findViewById(R.id.countdown_timer);
//        mCountdownTimer = new TextView(context);
    }

    public void setTextColor(int color) {
        mCountdownTimer.setTextColor(color);
    }

    public void setTimer(long leftTime, final CountDownTimerListener listener) {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }

        //第一个参数表示 总的时间为毫秒,间隔1000毫秒
        timer = new CountDownTimer(leftTime * 1000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                millisUntilFinished = millisUntilFinished / 1000;
                if (listener != null) {
                    listener.onTick(millisUntilFinished);
                }
                final long date = millisUntilFinished / 86400;
                final long hour = (millisUntilFinished % (24 * 60 * 60)) / (60 * 60);
                final long minute = (millisUntilFinished % 3600) / 60;
                final long second = (millisUntilFinished % 3600) % 60;
                mCountdownTimer.setText(date + ":" + toTwoNum(hour) + ":" + toTwoNum(minute) + ":" + toTwoNum(second));
            }

            @Override
            public void onFinish() {
                if (listener != null) listener.onFinish();
                timer.cancel();
            }
        };
        timer.cancel();
        timer.start();
    }

    public void setTimer(long leftTime, final boolean isUnit, final CountDownTimerListener listener) {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        //第一个参数表示 总的时间为毫秒,间隔1000毫秒
        timer = new CountDownTimer(leftTime * 1000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                millisUntilFinished = millisUntilFinished / 1000;
                if (listener != null) {
                    listener.onTick(millisUntilFinished);
                }
                long date = millisUntilFinished / 86400;
                long hour = (millisUntilFinished % (24 * 60 * 60)) / (60 * 60);
                long minute = (millisUntilFinished % 3600) / 60;
                long second = (millisUntilFinished % 3600) % 60;
                if (isUnit) {
                    mCountdownTimer.setText(date + "天" + toTwoNum(hour) + "小时" + toTwoNum(minute) + "分");
                } else {
                    mCountdownTimer.setText(date + ":" + toTwoNum(hour) + ":" + toTwoNum(minute) + ":" + toTwoNum(second));
                }

            }

            @Override
            public void onFinish() {
                if (listener != null) listener.onFinish();
                timer.cancel();
            }
        };
        timer.cancel();
        timer.start();
    }

    /**
     * 转换为两位数字
     *
     * @param num
     * @return
     */
    public String toTwoNum(long num) {
        DecimalFormat df = new DecimalFormat("00");
        return df.format(num);
    }


    public void cancelTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }


你可能感兴趣的:(关于listview中添加倒计时功能卡顿问题)