Android自定义view中,postDelayed执行失败

原因:在View的onAttachedToWindow之前进行post,会失败

解决方法

private boolean mAttached;

@Override
protected void onAttachedToWindow() {
      super.onAttachedToWindow();  
      mAttached = true;
}

 @Override
protected void onDetachedFromWindow() {
      mAttached = false;
      super.onDetachedFromWindow();
}

public void show() {
        //....
        if (mAttached) {
              postDelayed(mDelayedShow, MIN_DELAY);    
        } else {
              Handler handler = new Handler();
              handler.post(mDelayedShow);
        }
}

你可能感兴趣的:(Android自定义view中,postDelayed执行失败)