BottomSheet custom behavior - above BottomBar

此生再无相见时 提交于 2019-12-06 11:49:35

问题


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

There is what I tried to implement:

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

}

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?


回答1:


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


来源:https://stackoverflow.com/questions/40332965/bottomsheet-custom-behavior-above-bottombar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!