Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create bottomsheet with toolbar when expanded,

I want to create bottom sheet layout, which when expanded to full screen should display toolbar. I have used following code but it is displaying toolbar even if its not expanded to full screen.

<?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <include
        android:id="@+id/search_tab_toolbar"
        layout="@layout/search_tablayout"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/search_tab_toolbar"
        android:background="@color/accent"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />


</android.support.design.widget.CoordinatorLayout>
like image 827
Ragini Avatar asked Feb 05 '18 05:02

Ragini


1 Answers

You can use the setBottomSheetCallback from BottomSheetBehavior class.

Initially set the visibility of toobar as gone. Then in your code detect the state of the bottom sheet,

BottomSheetBehavior sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet); //your bottom sheet layout 

sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                switch (newState) {
                    case BottomSheetBehavior.STATE_HIDDEN:

                        break;
                    case BottomSheetBehavior.STATE_EXPANDED: 
                        //make toolbar visible
                        toolbar.setVisibility(View.VISIBLE);
                    break;
                    case BottomSheetBehavior.STATE_COLLAPSED:
                        //hide toolbar here
                        toolbar.setVisibility(View.GONE);
                    break;
                    case BottomSheetBehavior.STATE_DRAGGING:

                        break;
                    case BottomSheetBehavior.STATE_SETTLING:
                        break;
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });

Also, avoid doing stuff in STATE_DRAGGING and STATE_SETTLING. These will be called a lot of times and changing visibility in the these cases may decrease the performance of your app on lower end devices.

like image 184
Shantanu Avatar answered Nov 09 '22 17:11

Shantanu