隐藏后,浮动操作按钮(fab)行为在onSestedScroll上停止

时间:2017-02-06 13:27:54

标签: android floating-action-button

我为浮动操作按钮实现了一个简单的隐藏/显示行为。

调用onNestedScroll事件,直到在浮动操作按钮上调用hide()或setVisiblity(View.GONE),然后它停止对滚动事件做出反应。 当fab的可见性变为GONE时,它似乎停止对滚动事件作出反应。

public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {

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


@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout,
                                   final FloatingActionButton child,
                                   final View directTargetChild, final View target,
                                   final 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
            && child.getVisibility() == View.VISIBLE) {
        child.hide();
    } else if (dyConsumed < 0
            && child.getVisibility() != View.VISIBLE) {
                child.show();
            }
        }
    }
}

其他信息: 当我使用手动将visiblity设置为隐形时,它可以工作。但后来我错过了动画。

1 个答案:

答案 0 :(得分:10)

似乎可以更改隐藏确定的行为,如下所述:https://stackoverflow.com/a/41386278/1038102

child.hide(new FloatingActionButton.OnVisibilityChangedListener()
        @Override
        public void onHidden(FloatingActionButton fab) {
            super.onHidden(fab);
            fab.setVisibility(View.INVISIBLE);
        }
    });