mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-20 08:39:55 +08:00
am e152520f: am ef80bd54: Merge "Fix dependency sorting in CoordinatorLayout" into mnc-ub-dev
* commit 'e152520f897e91ec95e3ca558fbd78c5139879b8': Fix dependency sorting in CoordinatorLayout GitOrigin-RevId=e556b1b18ca90f61b653277fe3e043f27a9830cd PiperOrigin-RevId: 140550159
This commit is contained in:
parent
e3918bee1a
commit
0afb77643d
@ -566,7 +566,9 @@ public class CoordinatorLayout extends ViewGroup implements NestedScrollingParen
|
||||
for (int i = 0; i < childCount; i++) {
|
||||
mDependencySortedChildren.add(getChildAt(i));
|
||||
}
|
||||
Collections.sort(mDependencySortedChildren, mLayoutDependencyComparator);
|
||||
// We need to use a selection sort here to make sure that every item is compared
|
||||
// against each other
|
||||
selectionSort(mDependencySortedChildren, mLayoutDependencyComparator);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2637,4 +2639,37 @@ public class CoordinatorLayout extends ViewGroup implements NestedScrollingParen
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static void selectionSort(final List<View> list, final Comparator<View> comparator) {
|
||||
if (list == null || list.size() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
final View[] array = new View[list.size()];
|
||||
list.toArray(array);
|
||||
final int count = array.length;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
int min = i;
|
||||
|
||||
for (int j = i + 1; j < count; j++) {
|
||||
if (comparator.compare(array[j], array[min]) < 0) {
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
|
||||
if (i != min) {
|
||||
// We have a different min so swap the items
|
||||
final View minItem = array[min];
|
||||
array[min] = array[i];
|
||||
array[i] = minItem;
|
||||
}
|
||||
}
|
||||
|
||||
// Finally add the array back into the collection
|
||||
list.clear();
|
||||
for (int i = 0; i < count; i++) {
|
||||
list.add(array[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user