BottomSheetBehavior、AppBarLayout.ScrollingViewBehavior 和 AppBarLayout.Behavior 如何协同工作?

For*_*ice 5 android android-layout android-coordinatorlayout android-appbarlayout bottom-sheet

在下面的 GIF 中,您会看到我在协调 (heh)包含CollapsingToolbarLayoutPersistent BottomSheetAppBarLayout 时遇到了麻烦,它们可以很好地协同工作



目标: 片段内容,如上图绿松石色 ( #26999f ) 保持在上方,但滚动到下方,BottomSheet,如上图深绿色(#12783e),同时也尊重 AppBarLayout 及其行为

同样,正如您从 GIF 中看到的那样,我很接近;片段内容使用自定义 layout_behaviorMyScrollingViewBehavior,它扩展了AppBarLayout.ScrollingViewBehavior

在下面的代码片段中,如果是 a (本例中的BottomSheet)或任何( ) 依赖于的实例,您将看到MyScrollingViewBehavior#layoutDependsOn返回truedependencyRelativeLayoutsuperAppBarLayout.ScrollingViewBehavior#layoutDependsOn

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

MyScrollingViewBehavior#onDependentViewChanged如果dependency是的一个实例RelativeLayout,这意味着依赖是BottomSheet,孩子,又名片段的内容,可向上或向下移动使用offsetTopAndBottom,以确保它仍然是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;
}
Run Code Online (Sandbox Code Playgroud)

完整的代码示例可以在 Github 上找到。此外,这个问题是对我在这里的原始问题的后续和有希望的改进