如何在展开时使用工具栏创建底部图片

时间:2018-02-05 05:15:25

标签: 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>

2 个答案:

答案 0 :(得分:0)

您可以使用setBottomSheetCallback课程中的BottomSheetBehavior

最初将toobar的可见性设置为已消失。然后在您的代码中检测底部工作表的状态,

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) {

            }
        });

另外,避免在STATE_DRAGGING和STATE_SETTLING中执行操作。这些将被调用很多次,在这些情况下改变可见性可能会降低应用程序在低端设备上的性能。

答案 1 :(得分:0)

 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);  
       }