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>
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.
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