将 BottomSheetDialogFragment 的状态设置为展开

新手上路,请多包涵

如何使用 Android 支持设计库 (v23.2.1) 设置扩展 BottomSheetDialogFragment 以使用 BottomSheetBehavior#setState(STATE_EXPANDED) 扩展的片段的状态?

https://code.google.com/p/android/issues/detail?id=202396 说:

底部工作表首先设置为 STATE_COLLAPSED。如果要扩展它,请调用 BottomSheetBehavior#setState(STATE_EXPANDED)。请注意,您不能在视图布局之前调用该方法。

建议的做法 需要先膨胀视图,但我不确定如何将 BottomSheetBehaviour 设置到片段上( BottomSheetDialogFragment )。

 View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);

原文由 user2560886 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2k
2 个回答

“请注意,您不能在视图布局之前调用该方法。”

上面的文字是线索。

对话框有一个监听器,一旦 显示 对话框就会触发。如果未布局,则无法显示对话框。

因此,在模态底页( onCreateDialog() BottomSheetFragment )的 --- 中,就在返回对话框之前(或任何地方,一旦你有对对话框的引用),调用:

 // 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 结果是:

 @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;
    }
}

让我知道这是否有帮助。

更新

请注意,您还可以覆盖 BottomSheetDialogFragment 为:

 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 之外什么都不做。

ANDROIDX 更新

使用 AndroidX 时,之前在 android.support.design.R.id.design_bottom_sheet 找到的资源现在可以在 com.google.android.material.R.id.design_bottom_sheet 找到。

原文由 efemoney 发布,翻译遵循 CC BY-SA 4.0 许可协议

简单的答案(Kotlin + fragment + bottomSheetDialogViewBinding):

 val bsd = BottomSheetDialog(requireContext())
val bsdBinding = DialogBottomSheetViewBinding.inflate(LayoutInflater.from(requireContext()))
bsd.behavior.setState(BottomSheetBehavior.STATE_EXPANDED);

原文由 pravingaikwad07 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题