Fix ABL + CTL not handling being initially collapsed well

Two issues here. The first being that ABL only updates it's
min height after a layout, which means it's minHeight is wrong
in the preceding onMeasure(). Fixed by invalidating the scroll
ranges in onLayout, and updating it's minHeight in onMeasure.

Second issue is that CTL's text helper needs it's bounds in
the initial coordinate space, not after the views have been
offset.

Also fixed a possible pending state issue.

BUG: 23408061
Change-Id: Ie15c2e353da1596ca045405a6cf6f9cd2209a32e
diff --git a/design/src/android/support/design/widget/AppBarLayout.java b/design/src/android/support/design/widget/AppBarLayout.java
index 7c6e728..262caa0 100644
--- a/design/src/android/support/design/widget/AppBarLayout.java
+++ b/design/src/android/support/design/widget/AppBarLayout.java
@@ -204,6 +204,7 @@
     @Override
     protected void onLayout(boolean changed, int l, int t, int r, int b) {
         super.onLayout(changed, l, t, r, b);
+        invalidateScrollRanges();
 
         mHaveChildWithInterpolator = false;
         for (int i = 0, z = getChildCount(); i < z; i++) {
@@ -964,8 +965,6 @@
                         setAppBarTopBottomOffset(parent, abl, 0);
                     }
                 }
-                // Finally reset the pending state
-                abl.resetPendingAction();
             } else if (mOffsetToChildIndexOnLayout >= 0) {
                 View child = abl.getChildAt(mOffsetToChildIndexOnLayout);
                 int offset = -child.getBottom();
@@ -975,9 +974,12 @@
                     offset += Math.round(child.getHeight() * mOffsetToChildIndexOnLayoutPerc);
                 }
                 setTopAndBottomOffset(offset);
-                mOffsetToChildIndexOnLayout = INVALID_POSITION;
             }
 
+            // Finally reset any pending states
+            abl.resetPendingAction();
+            mOffsetToChildIndexOnLayout = INVALID_POSITION;
+
             // Make sure we update the elevation
             dispatchOffsetUpdates(abl);
 
diff --git a/design/src/android/support/design/widget/CollapsingToolbarLayout.java b/design/src/android/support/design/widget/CollapsingToolbarLayout.java
index 1f45098..3f5382e 100644
--- a/design/src/android/support/design/widget/CollapsingToolbarLayout.java
+++ b/design/src/android/support/design/widget/CollapsingToolbarLayout.java
@@ -362,6 +362,22 @@
     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
         super.onLayout(changed, left, top, right, bottom);
 
+        // Update the collapsed bounds by getting it's transformed bounds. This needs to be done
+        // before the children are offset below
+        if (mCollapsingTitleEnabled && mDummyView != null) {
+            ViewGroupUtils.getDescendantRect(this, mDummyView, mTmpRect);
+            mCollapsingTextHelper.setCollapsedBounds(mTmpRect.left, bottom - mTmpRect.height(),
+                    mTmpRect.right, bottom);
+            // Update the expanded bounds
+            mCollapsingTextHelper.setExpandedBounds(
+                    mExpandedMarginLeft,
+                    mTmpRect.bottom + mExpandedMarginTop,
+                    right - left - mExpandedMarginRight,
+                    bottom - top - mExpandedMarginBottom);
+
+            mCollapsingTextHelper.recalculate();
+        }
+
         // Update our child view offset helpers
         for (int i = 0, z = getChildCount(); i < z; i++) {
             final View child = getChildAt(i);
@@ -378,21 +394,6 @@
             getViewOffsetHelper(child).onViewLayout();
         }
 
-        // Update the collapsed bounds by getting it's transformed bounds
-        if (mCollapsingTitleEnabled && mDummyView != null) {
-            ViewGroupUtils.getDescendantRect(this, mDummyView, mTmpRect);
-            mCollapsingTextHelper.setCollapsedBounds(mTmpRect.left, bottom - mTmpRect.height(),
-                    mTmpRect.right, bottom);
-            // Update the expanded bounds
-            mCollapsingTextHelper.setExpandedBounds(
-                    mExpandedMarginLeft,
-                    mTmpRect.bottom + mExpandedMarginTop,
-                    right - left - mExpandedMarginRight,
-                    bottom - top - mExpandedMarginBottom);
-
-            mCollapsingTextHelper.recalculate();
-        }
-
         // Finally, set our minimum height to enable proper AppBarLayout collapsing
         if (mToolbar != null) {
             if (mCollapsingTitleEnabled && TextUtils.isEmpty(mCollapsingTextHelper.getText())) {