android - Manually measuring layout before used in recyclerview -


i wanted measure height of layouts ahead of them before used (in recyclerview). layout either have wrap_content size or specific dp set on (i.e. not dependent on parent view).

i made function below. works layout purely wrap_content size, , doesn't have layout within has defined size (i.e. wrap_content)

private float measureviewheight(int resource) {     view view = getlayoutinflater(null).inflate(resource, null);     view.measure(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content);     return (float) view.getmeasuredheight(); } 

but if layout containing layouts has specific dp height defined, measurement return not inaccurate. how should fix function returns right value?

update answer @shhp, , fixing prelollipop crash... updated answer use following

private float measureviewheight(int resource) {     view view = getlayoutinflater(null).inflate(resource, null);     view.setlayoutparams(new viewgroup.layoutparams(viewgroup.layoutparams.wrap_content,             viewgroup.layoutparams.wrap_content));     view.measure(view.measurespec.makemeasurespec(0, view.measurespec.unspecified),             view.measurespec.makemeasurespec(0, view.measurespec.unspecified));     return (float) view.getmeasuredheight(); } 

the parameters of view.measure have special meaning , should call measurespec.makemeasurespec parameter.

replace

view.measure(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); 

with

view.measure(measurespec.makemeasurespec(0, measurespec.unspecified), measurespec.makemeasurespec(0, measurespec.unspecified)); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -