Rag*_*ini 8 android toolbar android-collapsingtoolbarlayout bottom-sheet
我想创建底部工作表布局,当扩展到全屏时应该显示工具栏。我使用了以下代码,但它显示工具栏,即使它没有扩展到全屏。
<?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>
Run Code Online (Sandbox Code Playgroud)
您可以使用setBottomSheetCallbackfromBottomSheetBehavior类。
最初将工具栏的可见性设置为消失。然后在您的代码中检测底部工作表的状态,
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) {
}
});
Run Code Online (Sandbox Code Playgroud)
另外,避免在 STATE_DRAGGING 和 STATE_SETTLING 中进行操作。这些将被多次调用,在这些情况下改变可见性可能会降低应用程序在低端设备上的性能。