我正在使用这个精彩的library来创建FloatingActionMenu
。我在协调器布局中使用它:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/evaluations_list_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/evaluations_list_textview_time"
android:layout_below="@+id/evaluations_list_textview_name"
android:importantForAccessibility="no">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/evaluations_list_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:importantForAccessibility="no">
<android.support.v7.widget.RecyclerView
android:id="@+id/evaluations_list_row_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:importantForAccessibility="no"
android:padding="10dp"
android:textColor="@color/textcolorprimary" />
</android.support.v4.widget.SwipeRefreshLayout>
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/evaluations_list_floating_action_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/vector_drawable_ic_add_white"
app:layout_anchor="@id/evaluations_list_row_parent"
app:layout_anchorGravity="bottom|right|end"
app:layout_behavior="my.project.views.buttons.ScrollFAMBehaviour"
app:menu_animationDelayPerItem="50"
app:menu_colorNormal="@color/primaryColorDark"
app:menu_colorPressed="@color/accentColor"
app:menu_fab_size="normal"
app:menu_openDirection="up">
<com.github.clans.fab.FloatingActionButton
android:id="@+id/evaluations_list_floating_action_button_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/vector_drawable_ic_check_white"
app:fab_colorNormal="@color/primaryColorDark"
app:fab_colorPressed="@color/accentColor"
app:fab_label="@string/fab_filter"
app:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="@+id/evaluations_list_floating_action_button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/vector_drawable_ic_search_white"
app:fab_colorNormal="@color/primaryColorDark"
app:fab_colorPressed="@color/accentColor"
app:fab_label="@string/fab_search"
app:fab_size="mini" />
</com.github.clans.fab.FloatingActionMenu>
</android.support.design.widget.CoordinatorLayout>
现在我想创建一个CoordinatorLayout.Behavior
来隐藏用户向下滚动时的完整菜单,我想让它在用户向上滚动时再次显示。
Bevor我使用FloatingActionMenu
我使用了经典FloatingActionButton
。为此,使用以下代码很容易实现我想要的目标:
public class ScrollFABBehaviour extends FloatingActionButton.Behavior {
private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
private boolean isAnimatingOut = false;
public ScrollFABBehaviour(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
if (dyConsumed > 0 && !this.isAnimatingOut && child.getVisibility() == View.VISIBLE) {
this.animateOut(child);
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
this.animateIn(child);
}
}
private void animateOut(final FloatingActionButton button) {
ViewCompat.animate(button).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(new ViewPropertyAnimatorListener() {
public void onAnimationStart(View view) {
ScrollFABBehaviour.this.isAnimatingOut = true;
}
public void onAnimationCancel(View view) {
ScrollFABBehaviour.this.isAnimatingOut = false;
}
public void onAnimationEnd(View view) {
ScrollFABBehaviour.this.isAnimatingOut = false;
view.setVisibility(View.GONE);
}
}).start();
}
private void animateIn(FloatingActionButton button) {
button.setVisibility(View.VISIBLE);
ViewCompat.animate(button).scaleX(1.0F).scaleY(1.0F).alpha(1.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(null).start();
}
}
不幸的是,我无法在FloatingActionMenu
上使用此代码,因为它不是FloatingActionButton
,也不会继承它。这是一个自定义ViewGroup
。
我如何为FloatingActionMenu
创建这样的行为?
答案 0 :(得分:4)
这种行为实际上是通过创建CoordinatorLayout.Behavior<View>
获得的。因此,不要直接从FloatingActionButton.Behavior
延伸CoordinatorLayout.Behavior<FloatingActionMenu>
。
以下是适用于FloatingActionMenu
。
public class ScrollFAMBehaviour extends CoordinatorLayout.Behavior<FloatingActionMenu>{
private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
private boolean isAnimatingOut = false;
public ScrollFAMBehaviour(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionMenu child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionMenu child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
if (dyConsumed > 0 && !this.isAnimatingOut && child.getVisibility() == View.VISIBLE) {
this.animateOut(child);
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
this.animateIn(child);
}
}
private void animateOut(final FloatingActionMenu menu) {
ViewCompat.animate(menu).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(new ViewPropertyAnimatorListener() {
public void onAnimationStart(View view) {
ScrollFAMBehaviour.this.isAnimatingOut = true;
}
public void onAnimationCancel(View view) {
ScrollFAMBehaviour.this.isAnimatingOut = false;
}
public void onAnimationEnd(View view) {
ScrollFAMBehaviour.this.isAnimatingOut = false;
view.setVisibility(View.GONE);
}
}).start();
}
private void animateIn(FloatingActionMenu menu) {
menu.setVisibility(View.VISIBLE);
ViewCompat.animate(menu).scaleX(1.0F).scaleY(1.0F).alpha(1.0F).setInterpolator(INTERPOLATOR).withLayer().setListener(null).start();
}
}