如何在协调器布局内滚动时隐藏浮动操作按钮/菜单

时间:2018-01-14 22:38:18

标签: java android android-coordinatorlayout floating-action-button android-nestedscrollview

我希望在嵌套滚动视图中滚动时隐藏我的FloatingActionMenu。 我正在使用第三方库作为操作菜单,Clans-FloatingActionButton 我找到了layout_behavior的课程,但大多数方法是Deprecated 这是班级:

public class FabMenuBehavior extends CoordinatorLayout.Behavior<FloatingActionMenu> {

private int accumulator = 0;
private int threshold = 0;

public FabMenuBehavior() {
    super();
}

public FabMenuBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionMenu child, View directTargetChild, View target, int nestedScrollAxes) {
    threshold = (child.getChildCount() > 0 ? child.getChildAt(0).getHeight() : child.getHeight()) / 2;
    return true;
}

@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 ((accumulator * dyConsumed) < 0) { //scroll direction change
        accumulator = 0;
    }
    accumulator += dyConsumed;

    if (accumulator > threshold && !child.isMenuButtonHidden()) {
        child.hideMenuButton(true);
    } else if (accumulator < -threshold && child.isMenuButtonHidden()) {
        child.showMenuButton(true);
    }
}

@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionMenu child, View target) {
    super.onStopNestedScroll(coordinatorLayout, child, target);
    accumulator = 0;
}

}

那怎么能实现呢?谢谢。

0 个答案:

没有答案