我创建了一个自定义视图组。我在该组中实现了onLayout和onMeasured。但这两种方法都只被召唤一次,即第一次。当我删除子视图并添加新子项时,不会调用onLayout和onMeasured。任何帮助将不胜感激。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
// Iterate through all children, measuring them and computing our dimensions
// from their size.
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
// Measure the child.
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
}
}
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
setMeasuredDimension(displayMetrics.widthPixels,displayMetrics.heightPixels);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int count = getChildCount();
// These are the far left and right edges in which we are performing layout.
int topPosition = 0;
int leftPosition = 0;
int rightPosition = 0;
int bottomPosition = 0;
int i;
int j;
int decrementX = 0;
int incrementX = 0;
int verticalGap = 75;
int width;
int height;
View child;
if( count > 0 ){
int medium = count/2;
int gap = getMeasuredWidth()/count;
if( count == 1 ){
child = this.getChildAt(0);
width = child.getMeasuredWidth();
height = child.getMeasuredHeight();
leftPosition = getMeasuredWidth()/2 - width/2;
rightPosition = getMeasuredWidth() - ( getMeasuredWidth() - ( leftPosition + width ));
topPosition = 10;
bottomPosition = getMeasuredHeight() - ( getMeasuredHeight() - (topPosition + height));
child.layout(leftPosition,topPosition,rightPosition,bottomPosition);
}
else if( count%2 == 0 ){
child = this.getChildAt(0);
width = child.getMeasuredWidth();
height = child.getMeasuredHeight();
gap = getMeasuredWidth()/(count+1);
decrementX = incrementX = getMeasuredWidth()/2 - width/2;
for(i=medium,j=medium-1;(i<count && j>=0);i++,j--){
child = this.getChildAt(i);
width = child.getMeasuredWidth();
height = child.getMeasuredHeight();
decrementX -= gap;
leftPosition = decrementX;
rightPosition = getMeasuredWidth() - (getMeasuredWidth() - ( leftPosition + width ));
topPosition += verticalGap;
bottomPosition = getMeasuredHeight() - ( getMeasuredHeight() - ( topPosition + height ));
child.layout(leftPosition,topPosition,rightPosition,bottomPosition);
child = this.getChildAt(j);
incrementX += gap;
leftPosition = incrementX;
rightPosition = getMeasuredWidth() - ( getMeasuredWidth() - ( leftPosition + width ));
child.layout(leftPosition,topPosition,rightPosition,bottomPosition);
}
}
else if( count%3 == 0 || count%3 == 2 ){
child = this.getChildAt(medium);
if( child.getVisibility() != GONE ){
width = child.getMeasuredWidth();
height = child.getMeasuredHeight();
topPosition = 10;
leftPosition = getMeasuredWidth()/2 - width/2;
bottomPosition = getMeasuredHeight() - ( getMeasuredHeight() - ( topPosition + height ) );
rightPosition = getMeasuredWidth() - (getMeasuredWidth() - (leftPosition + width));
child.layout(leftPosition,topPosition,rightPosition,bottomPosition);
decrementX = leftPosition;
incrementX = leftPosition;
}
for(i=medium+1,j=medium-1;(i<count && j>=0); i++,j--){
child = this.getChildAt(i);
width = child.getMeasuredWidth();
height = child.getMeasuredHeight();
decrementX -= gap;
leftPosition = decrementX;
rightPosition = getMeasuredWidth() - ( getMeasuredWidth() - ( width + leftPosition ));
topPosition += verticalGap;
bottomPosition = getMeasuredHeight() - ( getMeasuredHeight() - (height + topPosition));
child.layout(leftPosition,topPosition,rightPosition,bottomPosition);
child = this.getChildAt(j);
width = child.getMeasuredWidth();
height = child.getMeasuredHeight();
incrementX += gap;
leftPosition = incrementX;
rightPosition = getMeasuredWidth() - ( getMeasuredWidth() - (width + leftPosition));
child.layout(leftPosition,topPosition,rightPosition,bottomPosition);
// final View leftView = getChildAt(j);
}
}
}
以上是onMeasure和onLayout方法。