How can BottomSheetBehavior, AppBarLayout.ScrollingViewBehavior, and AppBarLayout.Behavior work together in unison?

主宰稳场 提交于 2019-12-05 03:22:25

问题


In the GIF below you'll see I'm having trouble coordinating (heh) an AppBarLayout, containing a CollapsingToolbarLayout, and a Persistent BottomSheet in such a way that they play nicely together



Goal: Have the fragment contents, seen above in turquoise (#26999f), remain above, yet scroll behind, the BottomSheet, seen above in dark green (#12783e), while also respecting the AppBarLayout and its Behavior

Again, as you can see from the GIF I'm close; the fragment contents is using a custom layout_behavior, MyScrollingViewBehavior, which extends AppBarLayout.ScrollingViewBehavior

In the code snippet below you'll see MyScrollingViewBehavior#layoutDependsOn returns true if the dependency is an instance of either a RelativeLayout (The BottomSheet in this example) or whatever super (AppBarLayout.ScrollingViewBehavior#layoutDependsOn) is depentent on

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
    return dependency instanceof RelativeLayout ||
            super.layoutDependsOn(parent, child, dependency);
}

Within MyScrollingViewBehavior#onDependentViewChanged if the dependency is an instance of a RelativeLayout, meaning the dependency is the BottomSheet, the child, aka fragment contents, is moved up or down using offsetTopAndBottom to ensure it remains above the BottomSheet

@TargetApi(VERSION_CODES.LOLLIPOP)
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
    if (dependency instanceof RelativeLayout) {
        RelativeLayout bottomSheet = (RelativeLayout) parent.findViewById(R.id.bottom_sheet);
        int bottomSheetHeight = (bottomSheet.getTop() - child.getBottom());
        ViewCompat.offsetTopAndBottom(child, bottomSheetHeight);
    }
    super.onDependentViewChanged(parent, child, dependency);
    return false;
}

The full code sample can be found on Github here. Additionally, this question is a followup and hopeful improvement to my original question here

来源:https://stackoverflow.com/questions/40623168/how-can-bottomsheetbehavior-appbarlayout-scrollingviewbehavior-and-appbarlayou

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