CustomView で定義したフッターのバーに FloatingActionButton が乗っかるのがうざいのでFloatingActionButton.Behaviorをカスタマイズしてみた。
とりあえず、ソース。
public class AwesomeView extends ViewGroup {
public static class FabBehavior extends FloatingActionButton.Behavior {
private AwesomeView mAwesomeView;
public FabBehavior() {
}
public FabBehavior(Context context, AttributeSet attrs) {
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child,
View dependency) {
mAwesomeView = findAwesomeView(dependency);
return mAwesomeView != null || super.layoutDependsOn(parent, child, dependency);
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child,
View dependency) {
if (mAwesomeView != null) {
updateFabTranslationForAwesomeView(parent, child, mAwesomeView);
}
return super.onDependentViewChanged(parent, child, dependency);
}
private void updateFabTranslationForAwesomeView(CoordinatorLayout parent,
FloatingActionButton fab, View awesomeView) {
float translationY = this.getFabTranslationYForAwesomeView(parent, fab);
if (translationY != this.mTranslationY) {
ViewCompat.animate(fab).cancel();
if (Math.abs(translationY - this.mTranslationY) == (float) awesomeView.getHeight()) {
ViewCompat.animate(fab)
.translationY(translationY)
.setInterpolator(INTERPOLATOR)
.setListener(null);
} else {
ViewCompat.setTranslationY(fab, translationY);
}
this.mTranslationY = translationY;
}
}
private float getFabTranslationYForAwesomeView(CoordinatorLayout parent,
FloatingActionButton fab) {
final float offset = (mAwesomeView == null) ? .0f
: ViewCompat.getTranslationY(mAwesomeView) - (float) mAwesomeView.getHeight();
return Math.min(.0f, offset);
}
// TODO: 複数の AwesomeView 対応できるように
private AwesomeView findAwesomeView(View view) {
if (view instanceof AwesomeView) {
return (AwesomeView) view;
} else if (view instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) view;
for (int i = 0; i < group.getChildCount(); ++i) {
final AwesomeView awesomeView = findAwesomeView(group.getChildAt(i));
if (awesomeView != null) {
return awesomeView;
}
}
}
return null;
}
}
}
本来なら SnackBarLayout のようにレイアウトを定義し、その内の要素でレイアウトを行なうのだろうけど面倒くさいので。
