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

android – 自定义自动隐藏floatingActionButton行为不起作用,第1张

概述我试图在NestedScrollView向下滚动时隐藏FloatingActionButton,并在NestedScrollView向上滚动时显示自己. 这是我的布局: <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 我试图在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,int dxconsumed,int dyConsumed,int dxUnconsumed,int dyUnconsumed) {        super.onnestedScroll(coordinatorLayout,dxconsumed,dyConsumed,dxUnconsumed,dyUnconsumed);        if (dyConsumed > 0 && child.getVisibility() == VIEw.VISIBLE) {            child.hIDe();        } else if (dyConsumed < 0 && child.getVisibility() != VIEw.VISIBLE) {            child.show();        }    }}

这些代码对我不起作用,我想知道它是否与nestedScrollVIEw的行为有关.任何帮助将不胜感激!

UPDATE

我找到了有线的东西!如果我在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行为不起作用全部内容,希望文章能够帮你解决android – 自定义自动隐藏floatingActionButton行为不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1123226.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存