如果我使用Modal 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)
}
}
答案 0 :(得分:1)
谢谢大家!对我来说最好的解决方案是将我的BottomSheet作为持久性底部工作表添加到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
答案 1 :(得分:0)
您可以在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) {
}
};
答案 2 :(得分:0)
如果您对创建底页行为感兴趣,请按照以下步骤进行操作
您已在代码中设置了BottomSheetBehavior,如下面链接中所定义。
https://medium.com/@nullthemall/new-bottomsheet-caab21aff19b