Set state of BottomSheetDialogFragment to expanded

前端 未结 14 845
有刺的猬
有刺的猬 2020-11-30 19:11

How do you set the state of a fragment extending BottomSheetDialogFragment to expanded using BottomSheetBehavior#setState(STATE_EXPANDED) using the

14条回答
  •  借酒劲吻你
    2020-11-30 19:42

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    BottomSheetDialog d = (BottomSheetDialog) dialog;
    
                    FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
                    BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
                }
            });
    

    I met NullPointException in BottomSheetBehavior.from(bottomSheet) because d.findViewById(android.support.design.R.id.design_bottom_sheet) returns null.

    It's strange. I add this line of code to Watches in Android Monitor in DEBUG mode and found it return Framelayout normally.

    Here's code of wrapInBottomSheet in BottomSheetDialog:

    private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) {
            final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),
                    R.layout.design_bottom_sheet_dialog, null);
            if (layoutResId != 0 && view == null) {
                view = getLayoutInflater().inflate(layoutResId, coordinator, false);
            }
            FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(R.id.design_bottom_sheet);
            BottomSheetBehavior.from(bottomSheet).setBottomSheetCallback(mBottomSheetCallback);
            if (params == null) {
                bottomSheet.addView(view);
            } else {
                bottomSheet.addView(view, params);
            }
            // We treat the CoordinatorLayout as outside the dialog though it is technically inside
            if (shouldWindowCloseOnTouchOutside()) {
                coordinator.findViewById(R.id.touch_outside).setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (isShowing()) {
                                    cancel();
                                }
                            }
                        });
            }
            return coordinator;
        }
    

    Occasionally, I found that R.id.design_bottom_sheet is not equal to android.support.design.R.id.design_bottom_sheet. They have different value in different R.java.

    So I change android.support.design.R.id.design_bottom_sheet to R.id.design_bottom_sheet.

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    BottomSheetDialog d = (BottomSheetDialog) dialog;
    
                    FrameLayout bottomSheet = (FrameLayout) d.findViewById(R.id.design_bottom_sheet); // use R.java of current project
                    BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
                }
            });
    

    No more NullPointException now.

提交回复
热议问题