Android 来自模态BottomSheetFragment的BottomSheetCallback
如果使用模态BottomSheetFragment,如何添加BottomSheetBehaviorAndroid 来自模态BottomSheetFragment的BottomSheetCallback,android,kotlin,bottom-sheet,Android,Kotlin,Bottom Sheet,如果使用模态BottomSheetFragment,如何添加BottomSheetBehavior var bottomSheetBehaviorCallback = object : BottomSheetBehavior.BottomSheetCallback() { override fun onSlide(bottomSheet: View, slideOffset: Float) { Log.d(Constants.LOG_INFO,
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")
此时不创建视图
我更改了代码并添加了覆盖设置对话框。但我还是不能上班
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)
}
}
覆盖对话框(对话框:dialog?,样式:Int){
val contentView=View.inflate(上下文,R.layout.fragment\u station\u detail,null)
对话框!!.setContentView(contentView)
val layoutParams=(contentView.parent作为视图)。layoutParams作为CoordinatorLayout.layoutParams
val behavior=layoutParams.behavior
if(behavior!=null&&behavior为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) {
}
};
如果您对创建底部工作表行为感兴趣,请遵循以下步骤 您已经按照下面链接中的定义在代码中设置了BottomSheetBehavior
谢谢大家!对我来说,最好的解决方案是将我的底部工作表作为持久的底部工作表添加到activity_main.xml中 下面的解释对我很有帮助 最后,我找到了使用kotlin的Modal底板的解决方案
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
}
override fun onCreateDialog(savedInstanceState:Bundle?):Dialog{
val dialog=super.onCreateDialog(savedInstanceState)作为BottomSheetDialog
dialog.setOnShowListener{dialog->
val d=对话框作为底部对话框
val bottomSheet=d.findviewbyd(android.support.design.R.id.design\u bottom\u sheet)作为FrameLayout?
bottomSheetBehavior=bottomSheetBehavior.from(bottomSheet!!)
bottomSheetBehavior.state=bottomSheetBehavior.state\u已展开
}
返回对话框
}
我的问题是我试图使用我的协调器布局。但是对于模式底部表单,您必须使用android.support.design.R.id.design\u bottom\u sheet您的意思是BottomSheetFragment?是的。。谢谢,我改了,我没收到回电<代码>代码覆盖有趣的设置对话框(对话框:dialog?,样式:Int){val contentView=View.inflate(context,R.layout.fragment\u station\u detail,null)对话框!!.setContentView(contentView)val layoutParams=(contentView.parent作为视图).layoutParams as CoordinatorLayout.layoutParams val behavior=layoutParams.behavior如果(behavior!=null&&behavior是BottomSheetBehavior){behavior.setBottomSheetCallback(mBottomSheetBehaviorCallback)}
code
您的意思是setUpDialog方法不调用吗?更新您的代码您所做的将整个代码保存在BottomSheetFragment中,您如何从其他活动调用此片段