BottomSheet自定义行为-BottomBar上方 [英] BottomSheet custom behavior - above BottomBar

查看:149
本文介绍了BottomSheet自定义行为-BottomBar上方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to display BottomSheet above my BottomBar. So I have to write custom BottomSheet behaviorthat will put my BottomSheet above my BottomBar - the BottomBar has shy behavior (hidding during scrolling).

我试图实现以下目标:

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).

So far, this solution is not fully working. I can put the BottomSheet above BottomBar with the setY() method. But the expanding and collapsing is working wrong. So I tried to modify bottom of the BottomSheet with method setBottom() But it is not working neither. Maybe its because of wrong units (px vs dp).

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

Can anybody help me to fix my code or at least give me some hint what exactly I'm doing wrong or what I'm missing?

推荐答案

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

So I came out with my own solution. It is working perfectly fine, although there were some problems that had to be solved - such as the shadow above BottomBar or hidding BottomBar when BottomSheet is expadned etc..

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

For those who are facing the same or similar problem, there is my solution.

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

这篇关于BottomSheet自定义行为-BottomBar上方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆