内容简介:之前写过一篇FAB按钮的隐藏显示,代码注释中对关键的地方已经注释很清楚了,在不重复说明了,下面看看界面布局文件
之前写过一篇FAB按钮的隐藏显示, FloatingActionButton在RecycleView中滑动隐藏显示 ,它是通过监听RecycleView的滑动实现的,不过Google官方并没有推荐这种方式,今天看看比较官方的做法通过重写Behavior来实现。
public class FABBehavior extends FloatingActionButton.Behavior { private boolean visible = true;//是否可见 public FABBehavior(Context context, AttributeSet attrs) { super(); } //当观察的RecyclerView发生滑动开始的时候回调的 //axes滑动关联轴,我们现在只关心垂直的滑动 @Override public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) { return axes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type); } @Override public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FloatingActionButton child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) { super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type); if (dyConsumed > 0 && visible) { visible = false; animateOut(child); } else if (dyConsumed < 0 && !visible) { visible = true; animateIn(child); } } // FAB隐藏动画 private void animateOut(FloatingActionButton fab) { CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); fab.animate().translationY(fab.getHeight() + layoutParams.bottomMargin).setInterpolator(new AccelerateInterpolator(3)); ViewCompat.animate(fab).scaleX(0f).scaleY(0f).start(); } // FAB显示动画 private void animateIn(FloatingActionButton fab) { fab.animate().translationY(0).setInterpolator(new DecelerateInterpolator(3)); ViewCompat.animate(fab).scaleX(1f).scaleY(1f).start(); } } 复制代码
代码注释中对关键的地方已经注释很清楚了,在不重复说明了,
下面看看界面布局文件
<?xml version="1.0" encoding="utf-8"?> <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:tooltipText="" tools:context="com.mtx.floatingactionbutton.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rcv" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> <android.support.design.widget.FloatingActionButton android:id="@+id/fcb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/default_margin" android:backgroundTint="@color/colorAccent" android:onClick="onClickFAB" android:src="@drawable/up" app:borderWidth="0dp" app:elevation="10dp" app:fabSize="normal" app:layout_anchor="@id/rcv" app:layout_anchorGravity="bottom|right|end" app:layout_behavior="com.mtx.floatingactionbutton.FABBehavior" app:rippleColor="@color/colorPrimary" /> </android.support.design.widget.CoordinatorLayout> 复制代码
CoordinatorLayout帮助协调定义在里面的view之间的动画,app:layout_behavior="com.mtx.floatingactionbutton.FABBehavior" 这一行是重点,app:layout_behavior的值为我们自定义的类,ayout_anchor 与 layout_anchorGravity 属性就可以了,layout_anchor 指定参照物, anchorGravity 指定相对于参照物的位置,设置为 bottom|right则表示将FloatingActionButton放置于参照物的右下角。
注意点
- RecyclerView版本要是v22及以上,v21之前的版本不支持与CoordinatorLayout一起工作
- 和RecyclerView 22版本配合使用的时候会有一个已知的问题,如果滚动过快,会触发NullPointerExceptionbug连接,不过这个问题应该已经修复了,我在recyclerview-v7:27版本中快速滑动已经没有这个问题了
以上所述就是小编给大家介绍的《通过Behavior在RecycleView中隐藏显示FAB》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 通过可视化隐藏表示,更好地理解神经网络
- CycleGAN通过在生成的图像中隐藏信息来学习作弊
- 揭开隐藏算法的神秘面纱
- 2B产品的隐藏陷阱:销售驱动
- 应急响应之 Linux 下进程隐藏
- 隐藏在浏览器背后的 “黑手”
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
算法艺术与信息学竞赛
刘汝佳 / 清华大学出版社 / 2004-1 / 45.00元
《算法艺术与信息学竞赛》较为系统和全面地介绍了算法学最基本的知识。这些知识和技巧既是高等院校“算法与数据结构”课程的主要内容,也是国际青少年信息学奥林匹克(IOI)竞赛和ACM/ICPC国际大学生程序设计竞赛中所需要的。书中分析了相当数量的问题。 本书共3章。第1章介绍算法与数据结构;第2章介绍数学知识和方法;第3章介绍计算机几何。全书内容丰富,分析透彻,启发性强,既适合读者自学,也适合于课......一起来看看 《算法艺术与信息学竞赛》 这本书的介绍吧!