我已经使用这个 FloatingActionButton.Behavior 好几个月了,它负责隐藏和显示我的应用程序的 FAB。从来没有问题。
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
final View directTargetChild, final View target, final int nestedScrollAxes) {
// Ensure we react to vertical scrolling
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}
@Override
public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
final View target, final int dxConsumed, final int dyConsumed,
final int dxUnconsumed, final int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
// User scrolled down and the FAB is currently visible -> hide the FAB
child.hide();
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
// User scrolled up and the FAB is currently not visible -> show the FAB
child.show();
}
}}
一旦我更新到支持库
compile 'com.android.support:design:25.1.0'
FloatingActionButton.Behavior 停止正常工作。它隐藏 FAB 一次,然后再也不会调用它。不是 onStartNestedScroll 或 onNestedScroll 方法。
有谁知道这里发生了什么。我的其余代码保持不变,我只更新了这个库,它像以前一样停止工作。
最佳答案
对于 25.1.0
,CoordinatorLayout
在寻找要在其 onNestedScroll中调用的行为时跳过设置为
方法。GONE
的 View
解决方案是将 FAB 的可见性 GONE
替换为 INVISIBLE
。
简单地,改变:
child.hide();
到:
child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onHidden(FloatingActionButton fab) {
super.onHidden(fab);
fab.setVisibility(View.INVISIBLE);
}
});
关于Android 设计库 25.1.0 导致 FloatingActionButton.Behavior 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41761736/