将 BottomSheetDialogFragment 的状态设置为已展开 [英] Set state of BottomSheetDialogFragment to expanded
问题描述
如何使用 Android 支持设计库 (v23.2.1) 将扩展 BottomSheetDialogFragment
的片段的状态设置为使用 BottomSheetBehavior#setState(STATE_EXPANDED)
扩展?
How do you set the state of a fragment extending BottomSheetDialogFragment
to expanded using BottomSheetBehavior#setState(STATE_EXPANDED)
using the Android Support Design Library (v23.2.1)?
https://code.google.com/p/android/问题/详细信息?id=202396 说:
底部工作表首先设置为 STATE_COLLAPSED.如果要扩展它,请调用 BottomSheetBehavior#setState(STATE_EXPANDED).请注意,您不能在视图布局之前调用该方法.
Bottom sheets are set to STATE_COLLAPSED at first. Call BottomSheetBehavior#setState(STATE_EXPANDED) if you want to expand it. Note that you cannot call the method before view layouts.
建议的做法需要首先要膨胀的视图,但我不确定如何将 BottomSheetBehaviour 设置为片段 (BottomSheetDialogFragment
).
The suggested practice requires a view to be inflated first, but I'm not sure how I'll set the BottomSheetBehaviour onto a fragment (BottomSheetDialogFragment
).
View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
推荐答案
请注意,您不能在视图布局之前调用该方法."
"Note that you cannot call the method before view layouts."
以上文字为线索.
对话框有一个监听器,一旦对话框显示就会被触发.如果没有布局,则无法显示对话框.
Dialogs have a listener that is fired once the dialog is shown. The dialog cannot be shown if it isn't laid out.
因此,在模态底部表单 (BottomSheetFragment
) 的 onCreateDialog()
中,就在返回对话框之前(或任何地方,一旦您引用了对话框),调用:
So, in the onCreateDialog()
of your modal bottom sheet (BottomSheetFragment
), just before returning the dialog (or anywhere, once you have a reference to the dialog), call:
// This listener's onShow is fired when the dialog is shown
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
// In a previous life I used this method to get handles to the positive and negative buttons
// of a dialog in order to change their Typeface. Good ol' days.
BottomSheetDialog d = (BottomSheetDialog) dialog;
// This is gotten directly from the source of BottomSheetDialog
// in the wrapInBottomSheet() method
FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
// Right here!
BottomSheetBehavior.from(bottomSheet)
.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
就我而言,我的自定义 BottomSheet
结果是:
In my case, my custom BottomSheet
turned out to be:
@SuppressWarnings("ConstantConditions")
public class ShareBottomSheetFragment extends AppCompatDialogFragment {
@NonNull @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog =
new BottomSheetDialog(getActivity(), R.style.Haute_Dialog_ShareImage);
dialog.setContentView(R.layout.dialog_share_image);
dialog.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
SwitchCompat switchview = (SwitchCompat) dialog.findViewById(R.id.switchview);
switchview.setTypeface(FontCache.get(dialog.getContext(), lookup(muli, NORMAL)));
return dialog;
}
}
如果这有帮助,请告诉我.
Let me know if this helps.
更新
请注意,您还可以将 BottomSheetDialogFragment
覆盖为:
Note that you can also override BottomSheetDialogFragment
as:
public class SimpleInitiallyExpandedBottomSheetFragment extends BottomSheetDialogFragment {
@NonNull @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
// Do something with your dialog like setContentView() or whatever
return dialog;
}
}
但我真的不明白为什么有人会想要这样做,因为基础 BottomSheetFragment
除了返回 BottomSheetDialog
之外什么都不做.
But I really don't see why anyone would want to do that as the base BottomSheetFragment
doesn't do anything other than return a BottomSheetDialog
.
Android 更新
使用 AndroidX 时,以前在 android.support.design.R.id.design_bottom_sheet
中找到的资源现在可以在 com.google.android.material.R.id 中找到.design_bottom_sheet
.
When using AndroidX, the resource previously found at android.support.design.R.id.design_bottom_sheet
can now be found at com.google.android.material.R.id.design_bottom_sheet
.
这篇关于将 BottomSheetDialogFragment 的状态设置为已展开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!