Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在不添加xml布局的情况下将Behavior设置为底部工作表对话框?_Java_Android_Android Fragments - Fatal编程技术网

Java 如何在不添加xml布局的情况下将Behavior设置为底部工作表对话框?

Java 如何在不添加xml布局的情况下将Behavior设置为底部工作表对话框?,java,android,android-fragments,Java,Android,Android Fragments,我想动态显示BottomSheetDiyalogFragment,并在onClick事件时传递一些参数 InfoBottomSheetDialogFragment dialog = InfoBottomSheetDialogFragment.newInstance("param1", "param2"); dialog.show(getSupportFragmentManager(), "InfoBottomSheetDialogFragment

我想动态显示BottomSheetDiyalogFragment,并在onClick事件时传递一些参数

InfoBottomSheetDialogFragment dialog = InfoBottomSheetDialogFragment.newInstance("param1", "param2");
dialog.show(getSupportFragmentManager(), "InfoBottomSheetDialogFragment");
InfoBottomSheetDialogFragment dialog = InfoBottomSheetDialogFragment.newInstance("param1", "param2");
dialog.show(getSupportFragmentManager(), "InfoBottomSheetDialogFragment");
另外,我想将behavivor设置为片段

BottomSheetBehavior infoBtSheetBehaivor = BottomSheetBehavior.from(findViewById(R.id.info_bottom_sheet));
infoBtSheetBehaivor.setState(BottomSheetBehavior.STATE_COLLAPSED);
当我在主布局中包含片段xml布局时,如何到达它以传递参数?
或者,当我以编程方式添加片段时,如何设置其行为?有趣的情况。

您可以动态添加
BottomSheetDialogFragment
并在
setupDialog
时设置behavior,如下所示:

InfoBottomSheetDialogFragment
类中:

// Delete onCreate(Bundle savedInstanceState) method for avoiding double inflating.
@SuppressLint("RestrictedApi")
@Override
public void setupDialog(@NotNull Dialog dialog, int style) {
    super.setupDialog(dialog, style);

    View contentView = View.inflate(getContext(), R.layout.info_bottom_sheet, null);
    dialog.setContentView(contentView);

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

    if(behavior instanceof BottomSheetBehavior) {
        ((BottomSheetBehavior) behavior).setState(BottomSheetBehavior.STATE_HALF_EXPANDED);
    }
}
在需要的位置动态添加对话框并设置参数: