Custom android ViewGroup children's children are not laid out -
i've made very simple viewgroup subclass, "quadlayout", intended divide view's space equal quarters, , lay children out appropriately.
note, layout "dumb" - doesn't attempt calculate children's preferred heights or widths, no gravity, etc. sets 4 children top-left, top-right, bottom-left, & bottom-right quadrants. reason wrote layout because tablelayout & gridlayout refuse give me row heights. seem defer children height preferences, , instead of row height, 1 short row , 1 tall row.
now, children i'm laying out cardview subclasses, , inflate layout few textviews , imageview. simple layout each card.
here's problem.
when use tablelayout or gridlayout (with aforementioned row sizing issues) card's contents rendered.
when use quadlayout, cards correctly laid out, card's textviews, , imageviews not visible. i've stepped through debugger , confirmed card's layout being inflated, , textviews , imageviews being found , not-null. , appropriate model data being assigned.
i've overridden onlayout in cardview subclass, , i've confirmed while cardview assigned correct layout (left,top,right,bottom) children not laid out! have left/right/top/bottom of 0!
public class quadlayout extends viewgroup { private static final string tag = "quadlayout"; private rect childrect = new rect(); public quadlayout(context context) { super(context); setwillnotdraw(true); } public quadlayout(context context, attributeset attrs) { this(context, attrs, 0); setwillnotdraw(true); } public quadlayout(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); setwillnotdraw(true); } @override public boolean shoulddelaychildpressedstate() { // don't scroll children return false; } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { measurechildren(widthmeasurespec,heightmeasurespec); int width = measurespec.getsize(widthmeasurespec); int height = measurespec.getsize(heightmeasurespec); setmeasureddimension(width, height); } @override protected void onlayout(boolean changed, int left, int top, int right, int bottom) { int innerleft = this.getpaddingleft(); int innertop = this.getpaddingtop(); int innerright = this.getmeasuredwidth() - this.getpaddingright(); int innerbottom = this.getmeasuredheight() - this.getpaddingbottom(); int innerwidth = innerright - innerleft; int innerheight = innerbottom - innertop; int horizontalcenter = innerleft + (int) (math.ceil(innerwidth / 2f)); int verticalcenter = innertop + (int) (math.ceil(innerheight / 2f)); int count = getchildcount(); (int = 0; < count; i++) { view child = getchildat(i); if (child.getvisibility() != view.gone) { switch (i) { case 0: { childrect.left = innerleft; childrect.top = innertop; childrect.right = horizontalcenter; childrect.bottom = verticalcenter; break; } case 1: { childrect.left = horizontalcenter; childrect.top = innertop; childrect.right = innerright; childrect.bottom = verticalcenter; break; } case 2: { childrect.left = innerleft; childrect.top = verticalcenter; childrect.right = horizontalcenter; childrect.bottom = innerbottom; break; } case 3: { childrect.left = horizontalcenter; childrect.top = verticalcenter; childrect.right = innerright; childrect.bottom = innerbottom; break; } default: throw new illegalstateexception("quadlayout supports max of 4 children"); } layoutparams lp = (layoutparams) child.getlayoutparams(); childrect.left += lp.leftmargin; childrect.top += lp.topmargin; childrect.right -= lp.rightmargin; childrect.bottom -= lp.bottommargin; child.layout(childrect.left, childrect.top, childrect.right, childrect.bottom); } } } @override public layoutparams generatelayoutparams(attributeset attrs) { return new layoutparams(getcontext(), attrs); } @override protected layoutparams generatedefaultlayoutparams() { return new layoutparams(layoutparams.match_parent, layoutparams.match_parent); } @override protected viewgroup.layoutparams generatelayoutparams(viewgroup.layoutparams p) { return new layoutparams(p); } @override protected boolean checklayoutparams(viewgroup.layoutparams p) { return p instanceof layoutparams; } public static class layoutparams extends marginlayoutparams { public layoutparams(context c, attributeset attrs) { super(c, attrs); } public layoutparams(int width, int height) { super(width, height); } public layoutparams(viewgroup.layoutparams source) { super(source); } } } thanks in advance,
since nobody had better answer share hack divide screen 4 parts. implement bevaviour quadlayout - i'll explain basic idea in xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <view android:id="@+id/center" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerinparent="true" /> <android.support.v7.widget.cardview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_toleftof="@+id/center" android:layout_alignbottom="@+id/center"/> <android.support.v7.widget.cardview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_alignparentright="true" android:layout_torightof="@+id/center" android:layout_alignbottom="@+id/center"/> <android.support.v7.widget.cardview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_toleftof="@+id/center" android:layout_aligntop="@+id/center"/> <android.support.v7.widget.cardview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_torightof="@+id/center" android:layout_aligntop="@+id/center"/> </relativelayout> this should render properly, hope helps you!
Comments
Post a Comment