android - BottomSheet 自定义行为 - 在 BottomBar 之上

标签 android android-layout material-design android-coordinatorlayout bottom-sheet

我想在我的 BottomBar 上方显示 BottomSheet。所以我必须编写自定义 BottomSheet behavior 将我的 BottomSheet 放在 BottomBar 之上 - BottomBar 害羞的行为(滚动时隐藏)。

这是我试图实现的:

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);
    }
}

关于android - BottomSheet 自定义行为 - 在 BottomBar 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40332965/

相关文章:

css - 使用 Material Icon 添加自定义图标

javascript - Angular Material 布局演示无法正常工作

android - 无法在 TextView 中自动滚动文本

android - 启动画面后无法加载 Worklight 应用程序的页面

android - 如何在 Lollipop 5.0 的 WebView 中设置代理

android - ListView 向上滚动卡住

android - WebView 适用于 android 2.3.3,在 4.0.1 上显示空白页面

android - 更改 CheckedTextView 的 checkMark 或大小

android - 将 fragment 添加到 fragment

javascript - 日期选择器的自定义标题