作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用这个很棒的 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
来在用户向下滚动时隐藏完整的菜单,并且我希望它在用户向上滚动时再次显示。
在我使用 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
创建这样的行为?
最佳答案
这种行为实际上是通过创建 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();
}
}
关于android - 如何在 Scroll 的 CoordinatorLayout 中隐藏 FloatingActionMenu?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37230978/
我是一名优秀的程序员,十分优秀!