来自模态 BottomSheetFragment 的 BottomSheetCallback

BottomSheetCallback from Modal BottomSheetFragment

如果我使用模态 BottomSheetFragment,如何添加 BottomSheetBehavior?

var bottomSheetBehaviorCallback = 
    object : BottomSheetBehavior.BottomSheetCallback() {

        override fun onSlide(bottomSheet: View, slideOffset: Float) {
            Log.d(Constants.LOG_INFO, "on slide")
        }

        override fun onStateChanged(bottomSheet: View, newState: Int) {
            Log.d(Constants.LOG_INFO, "onStateChanged")
        }
    }

val detailView = StationDetailFragment.newInstance(selectedStationID, "")
detailView.show(fragmentManager, "detailView")

此时未创建视图

我更改了我的代码并添加了重写 setupDialog 。但是我仍然没有得到回调

override fun setupDialog(dialog: Dialog?, style: Int) {

    val contentView = View.inflate(context, R.layout.fragment_station_detail, null)
    dialog!!.setContentView(contentView)

    val layoutParams = (contentView.parent as View).layoutParams as CoordinatorLayout.LayoutParams

    val behavior = layoutParams.behavior

    if (behavior != null && behavior is BottomSheetBehavior<*>) {
      behavior.setBottomSheetCallback(mBottomSheetBehaviorCallback)
    }
}

您可以在setUpDialog 方法中设置您的行为。

 @Override
        public void setupDialog(Dialog dialog, int style) {
            View contentView = View.inflate(getContext(), R.layout.custom_filter_bottom_sheet, null);
            dialog.setContentView(contentView);
            CoordinatorLayout.LayoutParams layoutParams =
                    (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
            CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
            if (behavior != null && behavior instanceof BottomSheetBehavior) {
                ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
            }

        }

并定义回调

 private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                dismiss();
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        }
    };

如果您有兴趣创建底部 sheet 行为,请按照以下步骤操作

您已经按照下面 link 中的定义在代码中设置了 BottomSheetBehavior。

https://medium.com/@nullthemall/new-bottomsheet-caab21aff19b

谢谢大家!对我来说最好的解决方案是将我的 BottomSheet 添加为持久 Bottom Sheet 到 activity_main.xml

以下解释对我有帮助 Android working with Bottom Sheet

终于找到了使用 kotlin 的 Modal Bottomsheet 的解决方案

  override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {


    val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog

    dialog.setOnShowListener { dialog ->
        val d = dialog as BottomSheetDialog

        val bottomSheet = d.findViewById<View>(android.support.design.R.id.design_bottom_sheet) as FrameLayout?
        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet!!)
        bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
    }

    return dialog

}

我的问题是我试图使用我的协调器布局。但是对于 Modal BottomSheet 你必须使用 android.support.design.R.id.design_bottom_sheet