android - BottomSheetDialogFragment setupDialog 仅限于库组

标签 android bottom-sheet

我正在使用 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 documentation 中所展示的那样(BottomSheetDialogFragment 扩展),您应该使用 onCreateView() 来设置您的对话框。

您从此方法返回的View 将被设置为onCreateDialog() 提供的对话框的内容 View 。并且可以在 onCreateView() 中使用 getDialog() 方法对上述 Dialog 进行任何调整。


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


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

关于android - BottomSheetDialogFragment setupDialog 仅限于库组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47267301/

相关文章:

android - 如何使用 Sugar Orm 升级数据库?

java - 当EditText中没有任何内容时Android程序崩溃

java - 带有内部嵌套 ScrollView 的 Recyclerview?

android - OnSwipe dragUp 不适用于 MotionLayout 中的 subview

android - 仅在单击按钮时才需要显示 Bottom Sheet 行为?

java - BottomSheetDialogFragment关闭监听器

android - 以编程方式膨胀 TextView 和 LinearLayout

Android Sqlite 连接两列

Flutter:带有 TextField/TextFormField 的 Bottom Sheet

animation - 在 BottomSheet 中实现转换