Bottomsheetbehavior会在底部表格的顶部添加额外的空间

问题描述 投票:3回答:1

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

app:behavior_hideable="true"

app:behavior_peekHeight="128dp"After Adding BottomSheetBehaviorBefore 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);
        }

    }
android android-fragments material-design android-coordinatorlayout bottom-sheet
1个回答
0
投票
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() 
© www.soinside.com 2019 - 2024. All rights reserved.