【Material Design】自定义FloatingActionButton滑动行为只隐藏不出现的问题
来源:互联网 发布:全球产品样本数据库 编辑:程序博客网 时间:2024/05/16 11:17
先来段Behavior代码,网上关于FloatingActionButton(以下简称FAB)滑动的代码很多了,参考一下。
public class FabBehavior extends FloatingActionButton.Behavior{ private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator(); private boolean mIsAnimatingOut = false; public FabBehavior(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 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) { // User scrolled down and the FAB is currently visible -> hide the FAB animateOut(child); } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) { // User scrolled up and the FAB is currently not visible -> show the FAB animateIn(child); } } // Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits private void animateOut(final FloatingActionButton button) { if (Build.VERSION.SDK_INT >= 14) { ViewCompat.animate(button).translationY(button.getHeight() + getMarginBottom(button)).setInterpolator(INTERPOLATOR).withLayer() .setListener(new ViewPropertyAnimatorListener() { public void onAnimationStart(View view) { FabBehavior.this.mIsAnimatingOut = true; } public void onAnimationCancel(View view) { FabBehavior.this.mIsAnimatingOut = false; } public void onAnimationEnd(View view) { FabBehavior.this.mIsAnimatingOut = false; view.setVisibility(View.GONE); } }).start(); } else { } } // Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters private void animateIn(FloatingActionButton button) { button.setVisibility(View.VISIBLE); if (Build.VERSION.SDK_INT >= 14) { ViewCompat.animate(button).translationY(0) .setInterpolator(INTERPOLATOR).withLayer().setListener(null) .start(); } else { } } private int getMarginBottom(View v) { int marginBottom = 0; final ViewGroup.LayoutParams layoutParams = v.getLayoutParams(); if (layoutParams instanceof ViewGroup.MarginLayoutParams) { marginBottom = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin; } return marginBottom; }}
这是自定义的一个Behavior类,主要在onNestedScroll中自定义了出现和消失的动画。使用的时候,在xml文件中给FAB加一个包含完整behavior类名的layout_behavior属性
app:layout_behavior="com.normalframe.widgets.view.FabBehavior"
这样FAB就会随着列表上滑消失,下滑出现。这个功能主要是要处理FAB的位置会使列表最后一项被挡住的问题,当上滑时,FAB隐藏,这样当到达列表底部最后一项时,由于刚刚的动作是上滑动作,所以FAB处于隐藏状态,不会遮挡到列表。
在写这个功能时,发现了一个问题:
上滑时FAB能够正常隐藏,但是下拉列表时,FAB就不出现了。
而一样的代码如果放到其它项目中,有些又可以正常实现功能。Debug的时候发现,上拉时会调用onNestedScroll,于是其中自定义的隐藏方法可以被调用,但下滑时,不调用onNestedScroll。
最后发现是SDK版本的问题,用25(7.1.1)的时候会有这个问题,调成24就没问题了。。暂时还不知道原因,待研究。
5 0
- 【Material Design】自定义FloatingActionButton滑动行为只隐藏不出现的问题
- 关于解决自定义FloatingActionButton滑动行为(Behavior)只隐藏不出现的问题
- 【Android】(已更新解决方案)更新SDK版本(25)后,自定义FloatingActionButton的Behavior(跟随列表滑动显示隐藏)只隐藏不出现的问题
- FloatingActionButton自定义滑动动画,只隐藏不显示解决方案
- FloatingActionButton+RecycleView联动,并解决只隐藏不出现的bug
- FloatingActionButton属性、用法,以及解析并解决sdk25以上只隐藏不显示的问题
- Material Design之FloatingActionButton的使用
- Material Design之FloatingActionButton的使用
- Material Design之FloatingActionButton的使用
- Material Design之FloatingActionButton的使用
- Material Design -- FloatingActionButton
- Material Design之一:FloatingActionButton
- Material Design控件之FloatingActionButton
- Material Design控件之一FloatingActionButton
- Android Material Design之FloatingActionButton
- Material Design控件之FloatingActionButton
- Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout
- Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout
- 第三方(ShareSDK)分享和登录遇到的一些问题
- java异常的基本理解
- github android 资源
- 【java 基础 9】原来我从没有了解过String类
- PHP 快速排序算法
- 【Material Design】自定义FloatingActionButton滑动行为只隐藏不出现的问题
- 笔记:ffmpeg的AVFormatContext结构体
- PADS层说明
- 添加cyclictest 库文件到docker
- 检查服务器状态的软件--Keepalived基础知识
- MVP+Databinding模式开发APP(二)
- MVP模式简单理解
- Java小函数(1)
- 微服务接口(API)的过去,现在与未来