1

I'm trying to implement ViewGroup that estimates the number of child views that it is able to contain and fills itself with the previously estimated number of child views considering size's constraints provided by parent. I overrided onAttachToWindow callback to inflate and add first child view. I have ended up with this:

public class CustomViewGroup extends ViewGroup {
    private LayoutInflater layoutInflater;

    public FilledLayout(Context context) {
        super(context);
        initView(context);
    }

    public FilledLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public FilledLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    protected void onAttachedToWindow() {
        layoutInflater.inflate(R.layout.view_layout, this, true);

        super.onAttachedToWindow();
    }

I do not know how to properly overrideonMeasure callback to measure and calculate the number of times the layout of child view should be inflated. Is it a good idea to inflate layouts within onMeasure callback?

ghost
  • 87
  • 1
  • 9

0 Answers0