BottomSheet 自定义行为 - 在 BottomBar 之上

BottomSheet custom behavior - above BottomBar

我想在 BottomBar 上方显示 BottomSheet。所以我必须编写自定义 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;
}

}

到目前为止,这个解决方案还没有完全奏效。我可以使用 setY() 方法将 BottomSheet 放在 BottomBar 上方。但是扩展和折叠是错误的。所以我尝试用方法 setBottom() 修改 BottomSheet 的底部,但它也不起作用。可能是因为单位错误(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);
    }
}