首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BottomSheet自定义行为- BottomBar之上

BottomSheet自定义行为- BottomBar之上
EN

Stack Overflow用户
提问于 2016-10-30 19:53:03
回答 1查看 4.8K关注 0票数 3

我想在我的BottomSheet上面显示BottomBar。因此,我必须编写自定义的BottomSheet behavior,将我的BottomSheet放在BottomBar之上-- BottomBarshy behavior (滚动时隐藏)。

以下是我试图实现的:

代码语言:javascript
复制
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;
}

}

到目前为止,这一解决办法尚未完全奏效。我可以将BottomSheet放在BottomBar之上,并使用setY()方法。但是膨胀和崩溃是错误的。因此,我尝试用方法BottomSheet修改setBottom()底部,但两者都不能工作。也许是因为错误的单位(px对dp)。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-07 12:28:17

所以我拿出了自己的解决方案。虽然有一些问题必须解决--比如BottomBar上方的阴影或BottomSheet被删除时隐藏的BottomBar --但它仍然工作得很好。

对于那些面临同样或类似问题的人,我有解决办法。

代码语言:javascript
复制
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);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40332965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档