gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:15:04 25 4
gpt4 key购买 nike

我想在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com