您遵循的示例非常不走运。inside 的默认行为是FloatingActionButton
在CoordinatorLayout
显示时向上移动SnackBar
。由于此代码覆盖了Behavior
您失去此功能,因为这些方法从不调用它们的超类实现。显然作者没有考虑到这一点。但是,您可以修改ScrollingFABBehavior
以扩展原始文件Behavior
,从而支持SnackBar
:
public class ScrollingFABBehavior extends FloatingActionButton.Behavior {
private int toolbarHeight;
public ScrollingFABBehavior(Context context, AttributeSet attrs) {
super();
this.toolbarHeight = Utils.getToolbarHeight(context);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
return super.layoutDependsOn(parent, fab, dependency) || (dependency instanceof AppBarLayout);
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
boolean returnValue = super.onDependentViewChanged(parent, fab, dependency);
if (dependency instanceof AppBarLayout) {
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
int fabBottomMargin = lp.bottomMargin;
int distanceToScroll = fab.getHeight() + fabBottomMargin;
float ratio = (float)dependency.getY()/(float)toolbarHeight;
fab.setTranslationY(-distanceToScroll * ratio);
}
return returnValue;
}
}
这实际上是示例的github 存储库中的类,我自己编写了相同的代码并想对其进行测试后才找到它。他们只是忘记更新博客文章:-/