Reputation: 775
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>
Upvotes: 10
Views: 5158
Reputation: 2355
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: {
//show toolbar
showToolBar(true);
break
}
break;
case BottomSheetBehavior.STATE_COLLAPSED: {
//hide toolbar
showToolBar(false);
break;
}
break;
case BottomSheetBehavior.STATE_DRAGGING:{
//hide toolbar
showToolBar(false);
break;}
case BottomSheetBehavior.STATE_SETTLING:{
showToolBar(false);
break;}
}
}
private void showToolBar(boolean show){
if(show)
appBarLayout.setExpanded(true, false);
else
appBarLayout.setExpanded(false, false);
}
Upvotes: 0
Reputation: 1221
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.
Upvotes: 2