gpt4 book ai didi

android - 自定义自动隐藏 floatingActionButton 行为不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:39:09 24 4
gpt4 key购买 nike

我试图在 NestedScrollView 向下滚动时隐藏 FloatingActionButton,并在 NestedScrollView 向上滚动时显示它自己。

这是我的布局:

<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>

这是我的 floatingActionButton 行为:

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 的行为有关。任何帮助将不胜感激!

更新

我发现了一些有线的东西!如果我在 onNestedScroll 中调用 fab 的方法(child.hide()、child.show()),onStartNestedScroll 和 onNestedScroll 将不会再被调用,但如果我没有调用 fab 中的方法,onStartNestedScroll 和 onNestedScroll 将被正常调用。

最佳答案

看看@woxingxiao在说什么here

如果按钮的可见性为 GONE,几乎 onNestedScroll 不会被触发。所以,替换:

child.hide();

到:

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

关于android - 自定义自动隐藏 floatingActionButton 行为不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42782192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com