我想在BottomSheet上方显示BottomBar。因此,我必须编写自定义BottomSheet behavior,它将自己的BottomSheet放在我的BottomBar之上-BottomBar具有shy behavior(滚动期间隐藏)。

我尝试实现的是:

public class BottomSheetBehavior<T extends View> extends android.support.design.widget.BottomSheetBehavior<T> {

public BottomSheetBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
    return dependency instanceof BottomBar;
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
    // This will set the Y of my bottom sheet above the bottom bar every time BottomBar changes its position
    child.setY(dependency.getY() - child.getHeight());
    // But I also have to modify the bottom position of my BottomSheet
    // so the BottomSheet knows when its collapsed in its final bottom position.
    child.setBottom((int) dependency.getY() - dependency.getHeight());
    return false;
}

}

到目前为止,此解决方案尚未完全起作用。我可以使用BottomSheet方法将BottomBar放在setY()之上。但是扩张和崩溃是错误的。因此,我尝试使用BottomSheet方法修改setBottom()的底部,但是它都不起作用。可能是因为单位错误(px vs dp)。

有人可以帮助我修复我的代码,或者至少给我一些提示,说明我到底在做错什么或我在想什么?

最佳答案

所以我提出了自己的解决方案。尽管存在一些必须解决的问题,例如工作在BottomBar上方的阴影或在扩展BottomSheet时隐藏BottomBar等,它仍然可以正常工作。

对于那些面临相同或相似问题的人,有我的解决方案。

public class MyBottomSheetBehavior<T extends View> extends android.support.design.widget.BottomSheetBehavior<T> {

    private boolean mDependsOnBottomBar = true;

    public MyBottomSheetBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, T child, View dependency) {
        return (dependency instanceof BottomBar) || super.layoutDependsOn(parent, child, dependency);
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, T child, View dependency) {
        if (dependency instanceof BottomBar) {

            BottomBar bottomBar = (BottomBar) dependency;

            if (mDependsOnBottomBar) {
                //TODO this 4dp margin is actual shadow layout height, which is 4 dp in bottomBar library ver. 2.0.2
                float transitionY = bottomBar.getTranslationY() - bottomBar.getHeight()
                    + (getState() != STATE_EXPANDED ? Utils.dpToPixel(ContextProvider.getContext(), 4L) : 0F);
                child.setTranslationY(Math.min(transitionY, 0F));
            }

            if (bottomBar.getTranslationY() >= bottomBar.getHeight()) {
                mDependsOnBottomBar = false;
                bottomBar.setVisibility(View.GONE);
            }
            if (getState() != STATE_EXPANDED) {
                mDependsOnBottomBar = true;
                bottomBar.setVisibility(View.VISIBLE);
            }

            return false;
        }
        return super.onDependentViewChanged(parent, child, dependency);
    }
}

10-08 16:34