大佬教程收集整理的这篇文章主要介绍了Android自定义FloatingActionButton滑动行为只隐藏不出现的问题小结,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
先来段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 int dxConsumed,final int dyConsumed,final int dxUnconsumed,final int dyUnconsumed) { super.onnestedScroll(coordinatorLayout,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属性
这样FAB就会随着列表上滑消失,下滑出现。这个功能主要是要处理FAB的位置会使列表最后一项被挡住的问题,当上滑时,FAB隐藏,这样当到达列表底部最后一项时,由于刚刚的动作是上滑动作,所以FAB处于隐藏状态,不会遮挡到列表。
上滑时FAB能够正常隐藏,但是下拉列表时,FAB就不出现了。
而一样的代码如果放到其它项目中,有些又可以正常实现功能。debug的时候发现,上拉时会调用onnestedScroll,于是其中自定义的隐藏方法可以被调用,但下滑时,不调用onnestedScroll。
以上所述是小编给大家介绍的Android自定义FloaTingActionButton滑动行为只隐藏不出现的问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
以上是大佬教程为你收集整理的Android自定义FloatingActionButton滑动行为只隐藏不出现的问题小结全部内容,希望文章能够帮你解决Android自定义FloatingActionButton滑动行为只隐藏不出现的问题小结所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。