展开时如何使用工具栏创建底部表格,

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)

Sha*_*anu 2

您可以使用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 中进行操作。这些将被多次调用,在这些情况下改变可见性可能会降低应用程序在低端设备上的性能。

  • @shantanu 我想仅当底部工作表滚动到顶部时才显示工具栏。使用这种方法,即使底片的高度没有填满整个屏幕,它也会显示。 (2认同)