android - Fragments - onGobalLayout() called twice -
public class quadpadfragment extends fragment { int w = 0; int h = 0; public view oncreateview(layoutinflater inflater, final viewgroup container, bundle savedinstancestate) { final view view = inflater.inflate(r.layout.quadpadlayout, container, false); container.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { h = container.getmeasuredheight(); w = container.getmeasuredwidth(); log.w("quadpad fragment:------", "window width: " + w + " window height: " + h ); view.setlayoutparams(new layoutparams(1, 1)); } }); homecoming view; } }
i have above class written, works fine, puzzling me why ongloballayout called twice? im getting output log
:
w/quadpad fragment:------(27180): window width: 1080 window height: 1 w/quadpad fragment:------(27180): window width: 1080 window height: 1
i think it's because have setlayoutparams. phone call globallayout again. have unregister listener doens't called twice. above code remove listener.
@override public void ongloballayout() { if (build.version.sdk_int >= build.version_codes.jelly_bean) { container.getviewtreeobserver().removeongloballayoutlistener(this); } else { container.getviewtreeobserver().removeglobalonlayoutlistener(this); } h = container.getmeasuredheight(); w = container.getmeasuredwidth(); log.w("quadpad fragment:------", "window width: " + w + " window height: " + h ); view.setlayoutparams(new layoutparams(1, 1)); }
android layout size dimension
No comments:
Post a Comment