OnMeasure not getting a call in my regular android viewgroup

Im has two custom viewgroups , superViewGroup and subViewGroup . The subview group contains views. I am adding my supervisual group to linearLayout and subViewGroups to my superViewGroup .

The onMeasure() supervisor group receives the call, but not in the subview subgroup. but in both cases the onLayout() method gets called.

Code as follows

 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()); } } } } 

Comments are appreciated. thanks in advance.

+6
source share
1 answer

Because you must really pass the measure on to children's ideas:

 @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) } } } 

Otherwise, you never measure your children. It is up to you to do this. Just because your SuperViewGroup is in a linear layout, your SuperViewGroup takes responsibility for measuring its children.

+7
source

Source: https://habr.com/ru/post/899191/


All Articles