I want the toolbar to be pushed upwards and out of the view when bottom sheet is expanding and be pulled back in when it gets collapsed. All the examples I see are other views merging into the toolbar or snapping to toolbar to top but none of them hide the toolbar. How does one do that?
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<FrameLayout
android:id="@+id/mainContentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
/>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
>
<FrameLayout
android:id="@+id/topContentContainer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/blue"
app:layout_scrollFlags="scroll|enterAlways|snap"
/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/categoriesSelectionContainer"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/green"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="android.support.design.widget.AppBarLayout.ScrollingViewBehavior"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
/>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Just new behavior class:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--it can be any view-->
<android.support.v7.widget.Toolbar
android:id="@+id/appbarlayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_behavior="/*full path to class*/HidingViewWithBottomSheetBehavior">
</android.support.v7.widget.Toolbar>
<!--it can be any view-->
<FrameLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_peekHeight="50dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
</FrameLayout>
Full class:
public class HidingViewWithBottomSheetBehavior extends AppBarLayout.ScrollingViewBehavior {
private static final float UNDEFINED = Float.MAX_VALUE;
private float childStartY = UNDEFINED;
public HidingViewWithBottomSheetBehavior() {
}
public HidingViewWithBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return getBottomSheetBehavior(dependency) != null;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
BottomSheetBehavior bottomSheetBehavior = getBottomSheetBehavior(dependency);
if (bottomSheetBehavior != null) {
float slideOffset = getSlideOffset(parent, dependency, bottomSheetBehavior);
child.setAlpha(1 - slideOffset);
if (childStartY == UNDEFINED) {
childStartY = child.getY();
}
int childHeight = child.getMeasuredHeight();
float childY = childStartY - (childHeight * slideOffset);
child.setY(childY);
}
return true;
}
private float getSlideOffset(CoordinatorLayout parent, View dependency, BottomSheetBehavior bottomSheetBehavior) {
int parentHeight = parent.getMeasuredHeight();
float sheetY = dependency.getY();
int peekHeight = bottomSheetBehavior.getPeekHeight();
int sheetHeight = dependency.getHeight();
float collapseY = parentHeight - peekHeight;
float expandY = parentHeight - sheetHeight;
float deltaY = collapseY - expandY;
return (parentHeight - peekHeight - sheetY) / deltaY;
}
@Nullable
private BottomSheetBehavior getBottomSheetBehavior(@NonNull View view) {
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if (behavior instanceof BottomSheetBehavior) {
return (BottomSheetBehavior) behavior;
}
return null;
}
}
It simply push toolbar out and change visibility. If you want some more specific behavior, just rewrite onDependentViewChanged method.
If you want the bottom sheet to show above the toolbar, you can add android:elevation = 4dp
to the bottom sheet. This will make sure that when you expand the bottom sheet, it covers the toolbar. By this way, you can avoid the hassle of hiding/showing toolbar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With