【问题标题】:BottomSheet custom behavior - above BottomBarBottomSheet 自定义行为 - 在 BottomBar 上方
【发布时间】:2016-10-30 19:53:03
【问题描述】:

我想在我的BottomBar 上方显示BottomSheet。所以我必须编写自定义BottomSheetbehavior,这将把我的BottomSheet 放在我的BottomBar 之上——BottomBarshy 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)。

谁能帮我修复我的代码,或者至少给我一些提示我到底做错了什么或者我错过了什么?

【问题讨论】:

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


    【解决方案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 平移?我正在寻找一种方法来确保当底部工作表处于折叠状态时,我拥有的片段容器不会被底部工作表覆盖。任何想法表示赞赏!
    • 如何将自定义行为设置为底页?
    猜你喜欢
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    相关资源
    最近更新 更多