我上课了
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior { public ScrollAwareFABBehavior(Context context, AttributeSet attrs) { super(); } public ScrollAwareFABBehavior() { super(); } @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; } @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) { child.hide(); } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) { child.show(); } } }
问题是当我向上滚动一个recyclerview时,onNestedScroll只被调用一次,因此fab正在隐藏并且再也不会显示.这是我正在使用的布局
我的支持库版本是25.1.0
我解决了使用以下代码将可见性从GONE更改为INVISIBLE的问题.
@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(new FloatingActionButton.OnVisibilityChangedListener() { @Override public void onShown(FloatingActionButton fab) { super.onShown(fab); } @Override public void onHidden(FloatingActionButton fab) { super.onHidden(fab); fab.setVisibility(View.INVISIBLE); } }); } else if (dyConsumed <= 0 && child.getVisibility() != View.VISIBLE) { child.show(); } }
我刚刚 在另一篇文章中回答了同样的问题,请检查一下.
不久之后,请使用以下内容:
compile 'com.android.support:design:25.0.1'