浮动操作按钮在底部片断折叠状态下不显示

and*_*dXP 6 java android floating-action-button bottom-sheet

我有BottomSheetFragmentrecyclerview按钮fabFloating action button我在出现时遇到问题BottomSheetBehavior.STATE_COLLAPSED。当我将底部工作表扩展到全屏时,Fab 按钮就会出现,我尝试了几种方法,但没有任何效果。

我在布局中尝试了不同的 fab 选项以使其可见,但到目前为止还没有运气。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:id="@+id/topBarBottomSheet"
    android:clipToPadding="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/topBarBottomSheet">
        <include layout="@layout/progressbar_framelayout" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycleviewGallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingTop="@dimen/list_item_spacing_half"
            android:paddingBottom="@dimen/list_item_spacing_half"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:listitem="@layout/recycler_view_item_3"
            tools:spanCount="3"
            tools:layoutManager="GridLayoutManager" />



    </FrameLayout>
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabBottomSheet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:backgroundTint="@color/greenElaxer"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_check"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"

        android:overScrollMode="always"

         >

    </com.google.android.material.floatingactionbutton.FloatingActionButton>


</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

BottomSheetFragment

public class BottomSheet extends BottomSheetDialogFragment {

    FloatingActionButton fab;
    RecyclerView recyclerView;
    // 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) {

        return inflater.inflate(R.layout.fragment_bottemsheet_list_dialog, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        recyclerView = view.findViewById(R.id.recycleviewGallery);
        fab = view.findViewById(R.id.fabBottomSheet);     
      BottomSheetAdapter bottomSheetAdapter = new BottomSheetAdapter();
      GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),2);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(bottomSheetAdapter);
    }


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

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


}
Run Code Online (Sandbox Code Playgroud)

每当我折叠底板时,fab 按钮也会与底板一起向下。无论我展开还是折叠底板,我都需要使 fab 按钮粘在同一位置。提前致谢。

我需要坚持相同的布局(相对布局)

Fat*_*tih -1

当您退出某个片段时,您无法使该片段中的 UI 元素保留下来。如果底部工作表折叠,则其所有 UI 元素都会折叠。你不能坚持下去。

您可以编写一个重复的 fab,在关闭底层片段中的底页后显示该 fab。

例如,我在底页中有此代码用于退出底页对话框。

nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mBottomSheetDialog.dismiss();
            mTimePickerDialogController.show(0, 0, makeTimePickerDialogTag());
        }});
Run Code Online (Sandbox Code Playgroud)

当您关闭底页时,您可以使用以下代码使底层片段中的 fab 出现或消失。

mFloatingActionButton.hide();
mFloatingActionButton.show();
Run Code Online (Sandbox Code Playgroud)

编辑:处理第二个晶圆厂的更好解决方案。

BottomSheetBehavior sheetBehavior;
    sheetBehavior = BottomSheetBehavior.from(yourBottomSheet);

    sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            switch (newState) {
                case BottomSheetBehavior.STATE_HIDDEN:
                    mFloatingActionButton.show();
                case BottomSheetBehavior.STATE_EXPANDED: {
                    mFloatingActionButton.hide();
                }
                break;
                case BottomSheetBehavior.STATE_COLLAPSED: {
                    mFloatingActionButton.show();
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)