Ensure that CoL's sorted children is invalidated

Currently CoL only checks to see if the child count
has changed before re-sorting everything. This is a
bit crude as the children could change without the
count actually changing.

Fixed by invalidating the list on measure.

Also update dispatchDependentViewRemoved() to make
use of the dependency sorting.

BUG: 24252752

Change-Id: I92fd3a0587b7d24de0abcded37577cf70130d16c
diff --git a/design/src/android/support/design/widget/CoordinatorLayout.java b/design/src/android/support/design/widget/CoordinatorLayout.java
index 2ca18c3..6124ee8 100644
--- a/design/src/android/support/design/widget/CoordinatorLayout.java
+++ b/design/src/android/support/design/widget/CoordinatorLayout.java
@@ -548,28 +548,18 @@
     }
 
     private void prepareChildren() {
-        final int childCount = getChildCount();
-
-        boolean resortRequired = mDependencySortedChildren.size() != childCount;
-
-        for (int i = 0; i < childCount; i++) {
+        mDependencySortedChildren.clear();
+        for (int i = 0, count = getChildCount(); i < count; i++) {
             final View child = getChildAt(i);
-            final LayoutParams lp = getResolvedLayoutParams(child);
-            if (!resortRequired && lp.isDirty(this, child)) {
-                resortRequired = true;
-            }
-            lp.findAnchorView(this, child);
-        }
 
-        if (resortRequired) {
-            mDependencySortedChildren.clear();
-            for (int i = 0; i < childCount; i++) {
-                mDependencySortedChildren.add(getChildAt(i));
-            }
-            // We need to use a selection sort here to make sure that every item is compared
-            // against each other
-            selectionSort(mDependencySortedChildren, mLayoutDependencyComparator);
+            final LayoutParams lp = getResolvedLayoutParams(child);
+            lp.findAnchorView(this, child);
+
+            mDependencySortedChildren.add(child);
         }
+        // We need to use a selection sort here to make sure that every item is compared
+        // against each other
+        selectionSort(mDependencySortedChildren, mLayoutDependencyComparator);
     }
 
     /**
@@ -1147,15 +1137,23 @@
         }
     }
 
-    void dispatchDependentViewRemoved(View removedChild) {
-        final int childCount = getChildCount();
+    void dispatchDependentViewRemoved(View view) {
+        final int childCount = mDependencySortedChildren.size();
+        boolean viewSeen = false;
         for (int i = 0; i < childCount; i++) {
-            final View child = getChildAt(i);
-            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
-            final Behavior b = lp.getBehavior();
-
-            if (b != null && b.layoutDependsOn(this, child, removedChild)) {
-                b.onDependentViewRemoved(this, child, removedChild);
+            final View child = mDependencySortedChildren.get(i);
+            if (child == view) {
+                // We've seen our view, which means that any Views after this could be dependent
+                viewSeen = true;
+                continue;
+            }
+            if (viewSeen) {
+                CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)
+                        child.getLayoutParams();
+                CoordinatorLayout.Behavior b = lp.getBehavior();
+                if (b != null && lp.dependsOn(this, child, view)) {
+                    b.onDependentViewRemoved(this, child, view);
+                }
             }
         }
     }