底部弹出菜单自定义行为 - 在底部导航栏之上

3
我想在我的BottomBar上方显示BottomSheet。因此,我必须编写自定义的BottomSheetbehavior,将我的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)。请问有人能帮我修复代码或者至少给我一些提示,到底哪里出了问题或者我漏掉了什么?
1个回答

1

所以我想出了自己的解决方案。它完美地工作,尽管有一些问题需要解决,比如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);
    }
}

你会如何修改底部栏或底部表格的高度而不是x或y平移?我正在寻找一种方法,确保当底部表格处于折叠状态时,我的片段容器不被其覆盖。任何想法都将不胜感激! - Forrest Bice
3
如何为您的底部工作表设置自定义行为? - Daniel Silva

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接