Android自定义FloatingActionButton滑动行为只隐藏不出现的问题小结
先来段Behavior代码,网上关于FloatingActionButton(以下简称FAB)滑动的代码很多了,参考一下。
publicclassFabBehaviorextendsFloatingActionButton.Behavior{
privatestaticfinalInterpolatorINTERPOLATOR=newFastOutSlowInInterpolator();
privatebooleanmIsAnimatingOut=false;
publicFabBehavior(Contextcontext,AttributeSetattrs){
super();
}
@Override
publicbooleanonStartNestedScroll(finalCoordinatorLayoutcoordinatorLayout,finalFloatingActionButtonchild,
finalViewdirectTargetChild,finalViewtarget,finalintnestedScrollAxes){
//Ensurewereacttoverticalscrolling
returnnestedScrollAxes==ViewCompat.SCROLL_AXIS_VERTICAL
||super.onStartNestedScroll(coordinatorLayout,child,directTargetChild,target,nestedScrollAxes);
}
@Override
publicvoidonNestedScroll(finalCoordinatorLayoutcoordinatorLayout,finalFloatingActionButtonchild,
finalViewtarget,finalintdxConsumed,finalintdyConsumed,
finalintdxUnconsumed,finalintdyUnconsumed){
super.onNestedScroll(coordinatorLayout,child,target,dxConsumed,dyConsumed,dxUnconsumed,dyUnconsumed);
if(dyConsumed>0&&!this.mIsAnimatingOut&&child.getVisibility()==View.VISIBLE){
//UserscrolleddownandtheFABiscurrentlyvisible->hidetheFAB
animateOut(child);
}elseif(dyConsumed<0&&child.getVisibility()!=View.VISIBLE){
//UserscrolledupandtheFABiscurrentlynotvisible->showtheFAB
animateIn(child);
}
}
//SameanimationthatFloatingActionButton.BehaviorusestohidetheFABwhentheAppBarLayoutexits
privatevoidanimateOut(finalFloatingActionButtonbutton){
if(Build.VERSION.SDK_INT>=14){
ViewCompat.animate(button).translationY(button.getHeight()+getMarginBottom(button)).setInterpolator(INTERPOLATOR).withLayer()
.setListener(newViewPropertyAnimatorListener(){
publicvoidonAnimationStart(Viewview){
FabBehavior.this.mIsAnimatingOut=true;
}
publicvoidonAnimationCancel(Viewview){
FabBehavior.this.mIsAnimatingOut=false;
}
publicvoidonAnimationEnd(Viewview){
FabBehavior.this.mIsAnimatingOut=false;
view.setVisibility(View.GONE);
}
}).start();
}else{
}
}
//SameanimationthatFloatingActionButton.BehaviorusestoshowtheFABwhentheAppBarLayoutenters
privatevoidanimateIn(FloatingActionButtonbutton){
button.setVisibility(View.VISIBLE);
if(Build.VERSION.SDK_INT>=14){
ViewCompat.animate(button).translationY(0)
.setInterpolator(INTERPOLATOR).withLayer().setListener(null)
.start();
}else{
}
}
privateintgetMarginBottom(Viewv){
intmarginBottom=0;
finalViewGroup.LayoutParamslayoutParams=v.getLayoutParams();
if(layoutParamsinstanceofViewGroup.MarginLayoutParams){
marginBottom=((ViewGroup.MarginLayoutParams)layoutParams).bottomMargin;
}
returnmarginBottom;
}
}
这是自定义的一个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滑动行为只隐藏不出现的问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!