先来段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悬浮按钮基本使用、字体、颜色用法示例
# 自定义
# 小编
# 这是
# 就会
# 在此
# 就不
# 给大家
# 会使
# 一个问题
# 当上
# 所述
# 先来
# 给我留言
# 又可
# 感谢大家
# 出现了
# 主要是
# 表上
# 疑问请
# 有任何
相关文章:
公司网站设计制作厂家,怎么创建自己的一个网站?
如何规划企业建站流程的关键步骤?
ui设计制作网站有哪些,手机UI设计网址吗?
如何选择域名并搭建高效网站?
合肥制作网站的公司有哪些,合肥聚美网络科技有限公司介绍?
济南网站建设制作公司,室内设计网站一般都有哪些功能?
如何在服务器上三步完成建站并提升流量?
音乐网站服务器如何优化API响应速度?
建站之星如何实现PC+手机+微信网站五合一建站?
c++怎么编写动态链接库dll_c++ __declspec(dllexport)导出与调用【方法】
如何通过NAT技术实现内网高效建站?
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
高性价比服务器租赁——企业级配置与24小时运维服务
javascript中的try catch异常捕获机制用法分析
企业网站制作公司网页,推荐几家专业的天津网站制作公司?
建站主机系统SEO优化与智能配置核心关键词操作指南
网站企业制作流程,用什么语言做企业网站比较好?
网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?
如何选择高效可靠的多用户建站源码资源?
建站与域名管理如何高效结合?
常州自助建站工具推荐:低成本搭建与模板选择技巧
平台云上自助建站如何快速打造专业网站?
香港服务器租用每月最低只需15元?
小型网站建站如何选择虚拟主机?
如何用好域名打造高点击率的自主建站?
我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?
非常酷的网站设计制作软件,酷培ai教育官方网站?
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
建站之星如何取消后台验证码生成?
建站主机选购指南:核心配置与性价比推荐解析
网站app免费制作软件,能免费看各大网站视频的手机app?
婚礼视频制作网站,学习*后期制作的网站有哪些?
枣阳网站制作,阳新火车站打的到仙岛湖多少钱?
免费视频制作网站,更新又快又好的免费电影网站?
高端智能建站公司优选:品牌定制与SEO优化一站式服务
如何获取PHP WAP自助建站系统源码?
一键网站制作软件,义乌购一件代发流程?
临沂网站制作企业,临沂第三中学官方网站?
如何优化Golang Web性能_Golang HTTP服务器性能提升方法
正规网站制作公司有哪些,目前国内哪家网页网站制作设计公司比较专业靠谱?口碑好?
自助网站制作软件,个人如何自助建网站?
建站主机类型有哪些?如何正确选型
宝塔建站助手安装配置与建站模板使用全流程解析
代购小票制作网站有哪些,购物小票的简要说明?
网页设计网站制作软件,microsoft office哪个可以创建网页?
为什么Go需要go mod文件_Go go mod文件作用说明
网站设计制作企业有哪些,抖音官网主页怎么设置?
如何选择香港主机高效搭建外贸独立站?
Thinkphp 中 distinct 的用法解析
如何配置FTP站点权限与安全设置?
*请认真填写需求信息,我们会在24小时内与您取得联系。