BottomSheetDialogFragment setupDialog 仅限于库组

BottomSheetDialogFragment setupDialog restricted to library group

我正在使用支持库中的 BottomSheetDialogFragment,它警告我函数 setupDialog() 只能在库组中使用。但是这个函数是我初始化对话框的地方:

@Override
public void setupDialog(final Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    FragmentArgs.inject(this);

    dialog.setOnShowListener(dialogINterface -> {
        if(dialog.getWindow() != null) {
            dialog.getWindow().setLayout(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
        }
    });

    BottomSheetStatisticsExportBinding binding = DataBindingUtil.inflate(
            LayoutInflater.from(getContext()),
            R.layout.bottom_sheet_statistics_export,
            null,
            false
    );

    View contentView = binding.getRoot();
    dialog.setContentView(contentView);

    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
    CoordinatorLayout.Behavior behavior = params.getBehavior();

    if( behavior != null && behavior instanceof BottomSheetBehavior )
        ((BottomSheetBehavior) behavior).setBottomSheetCallback(bottomSheetBehaviorCallback);

    for (Export export : exports)
        binding.flexbox.addView(new ExportItemView(getContext(), export));
}

这个警告是因为我正在使用 super 方法。但是我应该怎么做呢?我是否应该将我的代码移到另一个函数中(onCreateDialog()onResume()...?),我是否应该删除对 super 的调用?

有人知道吗?

Should I move my code inside another function (onCreateDialog(), onResume()...?)

是的。正如它在 DialogFragment documentationBottomSheetDialogFragment 扩展)中所展示的那样,您应该使用 onCreateView() 来设置您的对话框。

您 return 通过此方法获得的 View 将被设置为 onCreateDialog() 提供的对话框的内容视图。并且可以在 onCreateView() 中使用 getDialog() 方法对上述 Dialog.

进行任何调整

onCreateDialog() 将用于替换默认值 Dialog。在您的情况下,您可能不想这样做;考虑到这是 BottomSheetDialogFragment 用来用 BottomSheetDialog 替换默认 Dialog 的方法(事实上,它是 BottomSheetDialogFragment 唯一覆盖的方法)。


Here 是我创建的 BottomSheetDialogBottomSheetDialogFragment 类 来替换支持库版本(有关更多信息,请参阅评论)。