问题描述
我试图在NestedScrollView向下滚动时隐藏FloatingActionButton,并在NestedScrollView向上滚动时显示自身.
I'm trying to hide a FloatingActionButton when a NestedScrollView scroll down, and revealed itself when NestedScrollView scroll up.
这是我的布局:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|enterAlways">
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/grid_2"
android:layout_gravity="bottom|end"
android:src="@drawable/ic_place_white"
android:clickable="true"
app:backgroundTint="@color/colorPrimary"
app:layout_behavior="com.myapp.ScrollAnimationFAB"/>
</android.support.design.widget.CoordinatorLayout>
这是我的floatActionButton行为:
And here is my floatingActionButton behavior:
public class ScrollAnimationFAB extends FloatingActionButton.Behavior {
public ScrollAnimationFAB(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 && child.getVisibility() == View.VISIBLE) {
child.hide();
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
child.show();
}
}
}
这些代码对我不起作用,我想知道它是否与NestedScrollView的行为有关.任何帮助将不胜感激!
These code is not working for me, I wonder if it has something to do with NestedScrollView's behavior. Any help will be appreciated!
更新
我发现有东西连接!如果我在onNestedScroll中调用fab的方法(child.hide(),child.show()),则永远不会再次调用onStartNestedScroll和onNestedScroll,但是如果我不在fab中调用方法,则onStartNestedScroll和onNestedScroll将被正常调用.
I found something wired! If I call fab's method (child.hide(), child.show()) in onNestedScroll, onStartNestedScroll and onNestedScroll never get called again, but if I didn't call methods in fab, onStartNestedScroll and onNestedScroll get called normally.
推荐答案
看看@woxingxiao在说什么
Take a look at what @woxingxiao is saying here
如果按钮的可见性为GONE
,几乎不会触发onNestedScroll
.因此,替换:
Pretty much onNestedScroll
won't get fired if the visibility of the button is GONE
. So, replace:
child.hide();
收件人:
child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onHidden(FloatingActionButton fab) {
super.onHidden(fab);
fab.setVisibility(View.INVISIBLE);
}
});
这篇关于自定义自动隐藏floatActionButton行为不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!