Android OnLayout和onMeasure未被调用

Android OnLayout和onMeasure未被调用,android,Android,我已经创建了一个自定义视图组。我在该组内部实现了onLayout和onMeasured。但这两种方法只被调用一次,即第一次。删除子视图并添加新的子视图时,不会调用onLayout和onMeasured。任何帮助都将不胜感激 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int count = getChildCount(); // Iter

我已经创建了一个自定义视图组。我在该组内部实现了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);
                }
            }
        }
以上是在线测量和在线布置方法