How to make bottom sheet push toolbar out when expanding and pull it back in when collapsed?
Asked Answered
C

3

5

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>
Crocodile answered 21/6, 2017 at 11:5 Comment(2)
you can use collapsing toolbar for that purposeFic
@NileshRathod can you be a bit more specific or point me to an example?Crocodile
C
6

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.

Cosher answered 21/10, 2017 at 12:5 Comment(1)
Great! Thanks! .. I had to modify your example to do nothing when the slideOffset is negative, which happens when the BottomSheet is hidden.Galleass
P
1

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.

Parette answered 26/2, 2018 at 9:31 Comment(0)
F
0

do few changes in your CoordinatorLayout in activity_main.XML like below(change theme as per your requirement).

<?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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

than :layout_behavior="@string/appbar_scrolling_view_behavior" inside android.support.v4.widget.NestedScrollView like below.

<android.support.v4.widget.NestedScrollView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello  "
            android:textSize="25dp"
            android:textStyle="bold" />

        /// Add your other code here

        </LinearLayout>

</android.support.v4.widget.NestedScrollView>
Fic answered 21/6, 2017 at 11:27 Comment(1)
This is not what I want to do, I have a bottomsheet on the bottom, a toolbar on the top. the toolbar needs to get pushed up/out of screen when bottom sheet expanded and pulled down/back into screen when collapsed.Crocodile

© 2022 - 2024 — McMap. All rights reserved.