我们有 app:layout_behavior =" @ string / appbar_scrolling_view_behavior" ;它的工作非常适合工具栏。 我创建自定义BottomBar并想要反转导致此标志的移动方向。 有什么想法如何实现它?
答案 0 :(得分:0)
我找到了解决方案!
我们需要像这样扩展CoordinatorLayout.Behavior
public class BottomBarBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {
private int defaultDependencyTop = -1;
public BottomBarBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, V child, View dependency) {
return dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, V child, View dependency) {
if (defaultDependencyTop == -1) {
defaultDependencyTop = dependency.getTop();
}
child.setTranslationY(-dependency.getTop() + defaultDependencyTop);
return true;
}
}
答案 1 :(得分:0)
Eduard Poida当隐藏底部导航时,当您更改手机的方向时,您的解决方案无法正常工作。Screenshot
我修改了一行解决方案,对我来说效果更好:
public class BottomNavigationBehavior<V extends View> extends CoordinatorLayout.Behavior<V>
{
private int defaultDependencyTop = -1;
public BottomNavigationBehavior(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, V child, View dependency)
{
return dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, V child, View dependency)
{
if (defaultDependencyTop == -1)
{
defaultDependencyTop = dependency.getTop();
}
child.setTranslationY(-dependency.getTop()*2);
return true;
}
}