Last active
August 29, 2015 14:23
-
-
Save eccyan/cdad78bf4eb86cc31fee to your computer and use it in GitHub Desktop.
FloatingActionButton.Behavior をカスタムして使ってみる ref: http://qiita.com/eccyan/items/cbfce9e94f81ecfaa452
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment