From 521aab5fcc600699ef102d06f953fe45f4edba1a Mon Sep 17 00:00:00 2001 From: Robert Spencer Date: Sun, 19 Feb 2017 23:57:34 -0800 Subject: [PATCH] Omptimisation on layout and measure children of YogaLayout Summary: You must measure children before you lay them out, and YogaLayout didn't do this. This fixes that. We also only recompute the yoga tree if the YogaLayout is laid out with a different size to what it's been measured as. Reviewed By: emilsjolander Differential Revision: D4572237 fbshipit-source-id: 6e86dbf939b06338c1dc2a37b7dafafd548dd390 --- .../com/facebook/yoga/android/YogaLayout.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/facebook/yoga/android/YogaLayout.java b/android/src/main/java/com/facebook/yoga/android/YogaLayout.java index 52607944..148488a2 100644 --- a/android/src/main/java/com/facebook/yoga/android/YogaLayout.java +++ b/android/src/main/java/com/facebook/yoga/android/YogaLayout.java @@ -260,6 +260,13 @@ public class YogaLayout extends ViewGroup { if (view.getVisibility() == GONE) { return; } + view.measure( + View.MeasureSpec.makeMeasureSpec( + Math.round(node.getLayoutWidth()), + View.MeasureSpec.EXACTLY), + View.MeasureSpec.makeMeasureSpec( + Math.round(node.getLayoutHeight()), + View.MeasureSpec.EXACTLY)); view.layout( Math.round(xOffset + node.getLayoutX()), Math.round(yOffset + node.getLayoutY()), @@ -286,10 +293,12 @@ public class YogaLayout extends ViewGroup { protected void onLayout(boolean changed, int l, int t, int r, int b) { // Either we are a root of a tree, or this function is called by our parent's onLayout, in which // case our r-l and b-t are the size of our node. - if (!(getParent() instanceof YogaLayout)) { - createLayout( - MeasureSpec.makeMeasureSpec(r - l, MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(b - t, MeasureSpec.EXACTLY)); + if (!(getParent() instanceof YogaLayout) && + Math.round(mYogaNode.getLayoutHeight()) != b-t && + Math.round(mYogaNode.getLayoutWidth()) != r-l) { + createLayout( + MeasureSpec.makeMeasureSpec(r - l, MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(b - t, MeasureSpec.EXACTLY)); } applyLayoutRecursive(mYogaNode, 0, 0);