Skip to content

Instantly share code, notes, and snippets.

@jpmcosta
Created July 20, 2015 14:55
Show Gist options
  • Save jpmcosta/4b46b504de9c4b1bb1f9 to your computer and use it in GitHub Desktop.
Save jpmcosta/4b46b504de9c4b1bb1f9 to your computer and use it in GitHub Desktop.
NOT a fix to CoordinatorLayout
/*
* Copyright 2015 João Martins Costa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;
import java.lang.reflect.Field;
import java.util.Comparator;
public class SimpleCoordinatorLayout extends CoordinatorLayout {
public SimpleCoordinatorLayout(Context context) {
this(context, null);
}
public SimpleCoordinatorLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SimpleCoordinatorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
try {
Field comparatorField = CoordinatorLayout.class.getDeclaredField("mLayoutDependencyComparator");
comparatorField.setAccessible(true);
Comparator<View> layoutDependencyComparator = (Comparator<View>) comparatorField.get(this);
comparatorField.set(this, new BehaviorComparator(layoutDependencyComparator));
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
private class BehaviorComparator implements Comparator<View> {
Comparator<View> mDefaultComparator;
public BehaviorComparator(Comparator<View> bla) {
mDefaultComparator = bla;
}
public int compare(View lhs, View rhs) {
int compare = mDefaultComparator.compare(lhs, rhs);
if (compare == 0 && lhs != rhs) {
// Null behavior means that the view doesn't depend on anything.
Behavior b1 = ((CoordinatorLayout.LayoutParams) lhs.getLayoutParams()).getBehavior();
Behavior b2 = ((CoordinatorLayout.LayoutParams) rhs.getLayoutParams()).getBehavior();
if (b1 == null && b2 != null) {
return -1;
} else if (b1 != null && b2 == null) {
return 1;
} else {
return 0;
}
} else {
return compare;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment