onMeasure не вызывается в моей пользовательской группе просмотра Android

У меня есть два пользовательских viewgroups, superViewGroup а также subViewGroup, Подгруппа содержит представления. Я добавляю свою суперпредставление к linearLayout и subViewGroups к моему superviewgroup,

Супер-группа onMeasure() вызывается, но не в подгруппе. но в обоих случаях onLayout() метод вызывается.

Код следующим образом

public class SuperViewGroup extends ViewGroup{

    public SuperViewGroup(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i("boxchart","INSIDE ON MEASURE SUPER VIEWGROUP");
    }



    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        final int count = getChildCount();

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                child.layout(0, 0, getWidth(), getHeight());

            }
        }


    }


}


public class SubViewGroup extends ViewGroup{

    public SubViewGroup(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i("boxchart","INSIDE ON MEASURE SUB VIEWGROUP");
    }



    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        final int count = getChildCount();

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                child.layout(0, 0, getWidth(), getHeight());

            }
        }


    }


}

Комментарии приветствуются. заранее спасибо.

1 ответ

Решение

Потому что вы должны передать меру представлениям детей:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    Log.i("boxchart","INSIDE ON MEASURE SUPER VIEWGROUP");
    final int count = getChildCount();

    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != View.GONE) {
            //Make or work out measurements for children here (MeasureSpec.make...)
            measureChild (child, widthMeasureSpec, heightMeasureSpec)
        }
    }
}

В противном случае вы никогда не будете измерять своих детей. Вам решать, как это сделать. Просто потому, что ваш SuperViewGroup в линейном макете, ваш SuperViewGroup берет на себя ответственность, чтобы измерить своих детей.

Другие вопросы по тегам