Android有用的工具类:倒计时器、 ViewHolder简化写法

倒计时器类:

Android自带CountDownTimer类很好的实现了倒数的功能

使用的时候只要继承并重写

  public abstract void onTick(long millisUntilFinished);

方法就能倒计时刷新view

public abstract void onFinish();

倒计时器结束时调用该方法刷新view


CountDownTimer 自带的handler如下:

 // handles counting down
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            synchronized (CountDownTimer.this) {
                final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();

                if (millisLeft <= 0) {
                    onFinish();
                } else if (millisLeft < mCountdownInterval) {
                    // no tick, just delay until done
                    sendMessageDelayed(obtainMessage(MSG), millisLeft);
                } else {
                    long lastTickStart = SystemClock.elapsedRealtime();
                    onTick(millisLeft);

                    // take into account user's onTick taking time to execute
                    long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();

                    // special case: user's onTick took more than interval to
                    // complete, skip to next interval
                    while (delay < 0) delay += mCountdownInterval;

                    sendMessageDelayed(obtainMessage(MSG), delay);
                }
            }
        }
    };

调用例子

	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
		super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
		}
		@Override
		public void onFinish() {//计时完毕时触发
		btnSms.setText(R.string.btn_send_checknum);
		btnSms.setEnabled(true);
		}
		@Override
		public void onTick(long millisUntilFinished){//计时过程显示
		btnSms.setEnabled(false);
		btnSms.setText(millisUntilFinished /1000+ getResources().getString(R.string.second));
		}
		}

使用方法:

	btnSms.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				new TimeCount(60000, 1000).start();
				//60秒。每一秒倒数一次
			}


ViewHolder简化

使用了Android自带的SparseArray,该类优化了HashMap,

代码:

public class ViewHolder {
    // I added a generic return type to reduce the casting noise in client code
    @SuppressWarnings("unchecked")
    public static  T get(View view, int id) {
        SparseArray viewHolder = (SparseArray) view.getTag();
        if (viewHolder == null) {
            viewHolder = new SparseArray();
            view.setTag(viewHolder);
        }
        View childView = viewHolder.get(id);
        if (childView == null) {
            childView = view.findViewById(id);
            viewHolder.put(id, childView);
        }
        return (T) childView;
    }
}


使用方法

@Override
public View getView(int position, View convertView, ViewGroup parent) {
 
    if (convertView == null) {
        convertView = LayoutInflater.from(context)
          .inflate(R.layout.banana_phone, parent, false);
    }
 
    ImageView bananaView = ViewHolder.get(convertView, R.id.banana);
    TextView phoneView = ViewHolder.get(convertView, R.id.phone);
 
    BananaPhone bananaPhone = getItem(position);
    phoneView.setText(bananaPhone.getPhone());
    bananaView.setImageResource(bananaPhone.getBanana());
 
    return convertView;
}


  开源项目:https://github.com/JoanZapata/base-adapter-helper,加入builder更为优化viewholder 



你可能感兴趣的:(SparseArray,CountDownTimer)