先来段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。
以上所述是小编给大家介绍的Android自定义FloatingActionButton滑动行为只隐藏不出现的问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
# floatingactionbutton
# android
# 滑动
# Android仿知乎悬浮功能按钮FloatingActionButton效果
# Android自定义可拖拽的悬浮按钮DragFloatingActionButton
# 修改Android FloatingActionButton的title的文字颜色及背景颜色实例详解
# Android中FloatingActionButton实现悬浮按钮实例
# Android悬浮按钮点击返回顶部FloatingActionButton
# Android 中FloatingActionButton(悬浮按钮)实例详解
# Android中Fab(FloatingActionButton)实现上下滑动的渐变效果
# Android中FloatingActionButton的显示与隐藏示例
# Android自定义APP全局悬浮按钮
# Android开发悬浮按钮 Floating ActionButton的实现方法
# Android开发之FloatingActionButton悬浮按钮基本使用、字体、颜色用法示例
# 自定义
# 小编
# 这是
# 就会
# 在此
# 就不
# 给大家
# 会使
# 一个问题
# 当上
# 所述
# 先来
# 给我留言
# 又可
# 感谢大家
# 出现了
# 主要是
# 表上
# 疑问请
# 有任何
相关文章:
Python多线程使用规范_线程安全解析【教程】
长春网站建设制作公司,长春的网络公司怎么样主要是能做网站的?
存储型VPS适合搭建中小型网站吗?
网站制作大概多少钱一个,做一个平台网站大概多少钱?
如何通过wdcp面板快速创建网站?
网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?
建站之星在线客服如何快速接入解答?
网站制作公司广州有几家,广州尚艺美发学校网站是多少?
网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?
如何选择域名并搭建高效网站?
寿县云建站:智能SEO优化与多行业模板快速上线指南
济南企业网站制作公司,济南社保单位网上缴费步骤?
制作销售网站教学视频,销售网站有哪些?
网站制作服务平台,有什么网站可以发布本地服务信息?
建站主机默认首页配置指南:核心功能与访问路径优化
贸易公司网站制作流程,出口贸易网站设计怎么做?
广东企业建站网站优化与SEO营销核心策略指南
成都网站制作报价公司,成都工业用气开户费用?
头像制作网站在线制作软件,dw网页背景图像怎么设置?
安云自助建站系统如何快速提升SEO排名?
建站中国官网:模板定制+SEO优化+建站流程一站式指南
建站中国必看指南:CMS建站系统+手机网站搭建核心技巧解析
香港服务器如何优化才能显著提升网站加载速度?
c++怎么编写动态链接库dll_c++ __declspec(dllexport)导出与调用【方法】
建站之星安装提示数据库无法连接如何解决?
代刷网站制作软件,别人代刷火车票靠谱吗?
济南网站建设制作公司,室内设计网站一般都有哪些功能?
小自动建站系统:AI智能生成+拖拽模板,多端适配一键搭建
SQL查询语句优化的实用方法总结
阿里云网站制作公司,阿里云快速搭建网站好用吗?
网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?
公司网站制作价格怎么算,公司办个官网需要多少钱?
实现点击下箭头变上箭头来回切换的两种方法【推荐】
油猴 教程,油猴搜脚本为什么会网页无法显示?
如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?
如何快速打造个性化非模板自助建站?
简单实现Android验证码
Swift中循环语句中的转移语句 break 和 continue
建站主机选哪种环境更利于SEO优化?
如何通过IIS搭建网站并配置访问权限?
在线流程图制作网站手机版,谁能推荐几个好的CG原画资源网站么?
微信推文制作网站有哪些,怎么做微信推文,急?
如何自定义建站之星模板颜色并下载新样式?
如何破解联通资金短缺导致的基站建设难题?
宿州网站制作公司兴策,安徽省低保查询网站?
如何快速生成专业多端适配建站电话?
,巨量百应是干嘛的?
香港服务器网站生成指南:免费资源整合与高速稳定配置方案
佛山企业网站制作公司有哪些,沟通100网上服务官网?
我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?
*请认真填写需求信息,我们会在24小时内与您取得联系。