我想在我的BottomSheet上面显示BottomBar。因此,我必须编写自定义的BottomSheet behavior,将我的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;
}
}到目前为止,这一解决办法尚未完全奏效。我可以将BottomSheet放在BottomBar之上,并使用setY()方法。但是膨胀和崩溃是错误的。因此,我尝试用方法BottomSheet修改setBottom()底部,但两者都不能工作。也许是因为错误的单位(px对dp)。
有人能帮我修复我的代码吗?或者至少给我一些提示,我到底做错了什么,或者我错过了什么?
发布于 2016-11-07 12:28:17
所以我拿出了自己的解决方案。虽然有一些问题必须解决--比如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);
}
}https://stackoverflow.com/questions/40332965
复制相似问题