Bottomsheetbehavior在底页顶部增加了额外的空间

时间:2019-12-28 12:48:05

标签: android android-fragments material-design android-coordinatorlayout bottom-sheet

我生成Android Studio's BottomSheetDialogFragment时可以正常工作,而无需进行任何修改,但是我将app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"添加到底部工作表进行回调时,会在底部工作表的顶部添加空白。我也尝试过

app:behavior_hideable="true"

app:behavior_peekHeight="128dp" After Adding BottomSheetBehavior Before adding BottomSheetBehavior

以及来自herehere的解决方案,但在我的情况下,这不适用于我的布局和片段

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

<RelativeLayout
    android:id="@+id/bottomSheet"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    >



    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/topBarBottomSheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Spinner
            android:id="@+id/attachmentOption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginLeft="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.019"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <SearchView
            android:id="@+id/searchView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/closeButton"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

        <ImageButton
            android:id="@+id/closeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:background="@android:drawable/ic_menu_close_clear_cancel" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <EditText
        android:id="@+id/searchViewEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/topBarBottomSheet"
        android:text="TextView" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleviewGallery"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:layout_below="@id/searchViewEditText"
        android:clipToPadding="false"
        android:paddingTop="@dimen/list_item_spacing_half"
        android:paddingBottom="@dimen/list_item_spacing_half"
        tools:listitem="@layout/recycler_view_item_3"
        tools:spanCount="3"
        tools:layoutManager="GridLayoutManager" />
</RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

BottomSheetDialogFragment

 public class BottomSheet extends BottomSheetDialogFragment {
        public static String TAG = "Bottom Sheet";


        // TODO: Customize parameter argument names
        private static final String ARG_ITEM_COUNT = "item_count";
        private Listener mListener;

        // TODO: Customize parameters
        public static BottomSheet newInstance() {
            /*final Bundle args = new Bundle();
            args.putInt(ARG_ITEM_COUNT, itemCount);
            fragment.setArguments(args);*/
            return new BottomSheet();
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
            getRecentImages();
            return inflater.inflate(R.layout.fragment_bottemsheet_list_dialog, container, false);
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            final RecyclerView recyclerView = view.findViewById(R.id.recycleviewGallery);
            View parentView = view.findViewById(R.id.bottomSheet);
            BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parentView);
            bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(@NonNull View view, int i) {
                    switch (i){
                        case BottomSheetBehavior.STATE_COLLAPSED:
                            Log.d(TAG,"Collapsed");
                            break;
                        case BottomSheetBehavior.STATE_DRAGGING:
                                Log.d(TAG,"Dragging");
                                break;
                        case BottomSheetBehavior.STATE_EXPANDED:
                            Log.d(TAG,"Expanded");
                            break;
                        case BottomSheetBehavior.STATE_HALF_EXPANDED:
                            Log.d(TAG,"Half Expanded");
                            break;
                        case BottomSheetBehavior.STATE_HIDDEN:
                            Log.d(TAG,"Hidden");
                            break;
                        case BottomSheetBehavior.STATE_SETTLING:
                            Log.d(TAG,"Settling");
                            break;

                    }
                }

                @Override
                public void onSlide(@NonNull View view, float v) {

                }
            });

        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            final Fragment parent = getParentFragment();
            Log.d(TAG,"Parent = "+parent+" Context "+context);
            if (parent != null) {
                mListener = (Listener) parent;
            } else {
                mListener = (Listener) context;
            }
        }

        @Override
        public void onDetach() {
            mListener = null;
            super.onDetach();
        }

        public interface Listener {
            void onBottomSheetClicked(int position);
        }

    }

1 个答案:

答案 0 :(得分:2)

我在不添加app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"的情况下使其底表完全正常工作,我从xml文件中删除了行为,然后在BottomSheetDialogFragment中像这样覆盖onCreateDialog

@NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                //Get the BottomSheetBehavior
                BottomSheetDialog d = (BottomSheetDialog) dialog;
                FrameLayout bottomSheet = d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
                if (bottomSheet != null) {
                    bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
                    bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
                    bottomSheet.setMinimumHeight(350);
                    bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                        @Override
                        public void onStateChanged(@NonNull View view, int i) {
                            switch (i){
                                case BottomSheetBehavior.STATE_COLLAPSED:
                                    Log.d(TAG,"Collapsed");

                                    break;
                                case BottomSheetBehavior.STATE_DRAGGING:
                                    Log.d(TAG,"Dragging");

                                    break;
                                case BottomSheetBehavior.STATE_EXPANDED:
                                    Log.d(TAG,"Expanded");

                                    break;
                                case BottomSheetBehavior.STATE_HALF_EXPANDED:
                                    Log.d(TAG,"Half Expanded");
                                    break;
                                case BottomSheetBehavior.STATE_HIDDEN:
                                    Log.d(TAG,"Hidden");
                                    dismiss();
                                    break;
                                case BottomSheetBehavior.STATE_SETTLING:
                                    Log.d(TAG,"Settling");
                                    break;

                            }
                        }

                        @Override
                        public void onSlide(@NonNull View view, float v) {

                        }
                    });
                }
            }
        });

        return dialog;
    }

如果您在上述onShow方法中的代码中注意到我正在创建BottomSheetDialog的实例,并借助实例为FrameLayout对象android(x)提供默认底表

BottomSheetDialog d = (BottomSheetDialog) dialog;
                FrameLayout bottomSheet = d.findViewById(com.google.android.material.R.id.design_bottom_sheet);

然后将帧布局的对象赋予BottomSheetBehavior

BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

,最后将回调添加到底部表单

bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback()