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