I want to show a BottomSheetDiyalogFragment dynamically with passing some parameters when a onClick event.
InfoBottomSheetDialogFragment dialog = InfoBottomSheetDialogFragment.newInstance("param1", "param2");
dialog.show(getSupportFragmentManager(), "InfoBottomSheetDialogFragment");
Also, I want to set behaivor to the fragment.
BottomSheetBehavior infoBtSheetBehaivor = BottomSheetBehavior.from(findViewById(R.id.info_bottom_sheet));
infoBtSheetBehaivor.setState(BottomSheetBehavior.STATE_COLLAPSED);
When I include the fragment xml layout on main layout, how to reach it for passing parameters? Or, when I add the fragment programatically, how to set behaivor it? Interesting situation.
You can add BottomSheetDialogFragment
dynamically and set behaivor when setupDialog
like this:
In your InfoBottomSheetDialogFragment
class:
// 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);
}
}
Add dialog dynamically where you want and set parameters:
InfoBottomSheetDialogFragment dialog = InfoBottomSheetDialogFragment.newInstance("param1", "param2");
dialog.show(getSupportFragmentManager(), "InfoBottomSheetDialogFragment");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.