当我编写一个自定义视图时,向其添加ViewTreeObserver.onGlobalLayoutListener,但是,我两次调用了监听器,并且方法的值更改了
public void setClassStatus(final ClassStatus s) {
Log.d("lijiwei321", "setClassStatus execute,input s:"+s);
setClassStatusFlag = true;
QualificationsSeekBarView.this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.d("lijiwei321", "currentThread:"+Thread.currentThread().getName()+"callback s:"+s);
if (setClassStatusFlag) {
int availableWidth = getMeasuredWidth() - getPaddingRight() - getPaddingLeft();
Log.d("availableWidth", "availableWidth:" + availableWidth);
if (s == ClassStatus.Eager) {
currentX = Math.round((shortLineLength - (radius1 - radius0)) / sumLength * availableWidth);
} else if (s == ClassStatus.Refine) {
currentX = Math.round((shortLineLength + radius0 + radius0 + commonLineLength - (radius1 - radius0)) / sumLength * availableWidth);
} else if (s == ClassStatus.TravelFar) {
currentX = Math.round((shortLineLength + radius0 + radius0 + commonLineLength
+ radius0 + radius0 + commonLineLength - (radius1 - radius0)) / sumLength * availableWidth);
} else {
currentX = Math.round((shortLineLength + radius0 + radius0 + commonLineLength
+ radius0 + radius0 + commonLineLength + radius0 + commonLineLength + radius0 - (radius1 - radius0)) / sumLength * availableWidth);
}
invalidate();
setClassStatusFlag = false;
}
}
});
}