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