我怎么能听到最终解雇BottomSheetDialogFragment
?我想在最终解雇时保存用户更改...
我试过以下:
方法1
如果通过向下滑动对话框解除对话(不在背面按压或外面触摸),则仅触发此操作
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog d = super.onCreateDialog(savedInstanceState);
d.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior behaviour = BottomSheetBehavior.from(bottomSheet);
behaviour.setState(BottomSheetBehavior.STATE_EXPANDED);
behaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN)
{
// Bottom Sheet was dismissed by user! But this is only fired, if dialog is swiped down! Not if touch outside dismissed the dialog or the back button
Toast.makeText(MainApp.get(), "HIDDEN", Toast.LENGTH_SHORT).show();
dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
}
});
return d;
}
方法2
这不允许我区分最终解雇和来自屏幕轮换或活动娱乐的解雇......
@Override
public void onDismiss(DialogInterface dialog)
{
super.onDismiss(dialog);
// this works fine but fires one time too often for my use case, it fires on screen rotation as well, although this is a temporarily dismiss only
Toast.makeText(MainApp.get(), "DISMISSED", Toast.LENGTH_SHORT).show();
}
题
如何收听表示用户已完成对话的事件?
虽然关于SO的所有类似问题建议使用onDismiss
,但我认为以下是正确的解决方案:
@Override
public void onCancel(DialogInterface dialog)
{
super.onCancel(dialog);
Toast.makeText(MainApp.get(), "CANCEL", Toast.LENGTH_SHORT).show();
}
如果:
* the user presses back
* the user presses outside of the dialog
这不会引发:
* on screen rotation and activity recreation
解
结合onCancel和BottomSheetBehavior.BottomSheetCallback.onStateChanged如下:
public class Dailog extends BottomSheetDialogFragment
{
@Override
public void onCancel(DialogInterface dialog)
{
super.onCancel(dialog);
handleUserExit();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog d = super.onCreateDialog(savedInstanceState);
d.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 behaviour = BottomSheetBehavior.from(bottomSheet);
behaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN)
{
handleUserExit();
dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
}
});
return d;
}
private void handleUserExit()
{
Toast.makeText(MainApp.get(), "TODO - SAVE data or similar", Toast.LENGTH_SHORT).show();
}
}
我用这个简单的技巧实现了这一点
val bottomSheetDialog = FeedbackFormsFragment.createInstance()
bottomSheetDialog.show((activity as FragmentActivity).supportFragmentManager, BOTTOM_SHEET)
// add some delay to allow the bottom sheet to be visible first so that the dialog is not null
Handler().postDelayed({
bottomSheetDialog.dialog?.setOnDismissListener {
// add code here
}
}, 1000)
虽然@ prom85的方法有效,但有一个不同的方法。如果你想在一个案件中解雇BottomSheetDialogFragment
而在另一个案件中保留,那么它将无效。它将在所有情况下关闭对话框。
例如,如果您在EditText
的BottomSheetDialogFragment
中输入文本并偶尔在外部单击,则会在没有任何警告的情况下关闭对话框。我试过https://medium.com/@anitas3791/android-bottomsheetdialog-3871a6e9d538,它有效,但在另一种情况下。向下拖动对话框时,它会将其关闭。如果单击外部,则不会显示任何警告消息,也不会忽略该对话框。
所以,我使用了来自https://stackoverflow.com/a/50734566/2914140的@shijo的好建议。
将这些行添加到onActivityCreated
方法(或onCreateView
之后的任何其他生命周期方法)。
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View touchOutsideView = getDialog().getWindow()
.getDecorView()
.findViewById(android.support.design.R.id.touch_outside);
touchOutsideView.setOnClickListener(yourClickListener);
}
在我的yourClickListener
案例中,我检查文本并显示警告或关闭对话框:
private fun checkAndDismiss() {
if (newText == oldText) {
dismissAllowingStateLoss()
} else {
showDismissAlert()
}
}
当你创建BottomSheetDialogFragment
时,不要像在setCancelable(false)
中那样调用https://stackoverflow.com/a/42679131/2914140,否则这些方法可能无效。也许不要在<item name="android:windowCloseOnTouchOutside">false</item>
中设置setCanceledOnTouchOutside(false)
样式或onCreateDialog
。
我还尝试了几种方法来覆盖取消行为,但它们没有成功。
<style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.BottomSheetDialogTheme)
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.setOnShowListener {
val bottomSheet = dialog.findViewById<View>(
android.support.design.R.id.design_bottom_sheet) as? FrameLayout
val behavior = BottomSheetBehavior.from(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
//showing the different states.
when (newState) {
BottomSheetBehavior.STATE_HIDDEN -> dismiss() //if you want the modal to be dismissed when user drags the bottomsheet down
BottomSheetBehavior.STATE_EXPANDED -> {
}
BottomSheetBehavior.STATE_COLLAPSED -> {
}
BottomSheetBehavior.STATE_DRAGGING -> {
}
BottomSheetBehavior.STATE_SETTLING -> {
}
}
}
})
dialog.setOnCancelListener {
// Doesn't matter what you write here, the dialog will be closed.
}
dialog.setOnDismissListener {
// Doesn't matter what you write here, the dialog will be closed.
}
}
return dialog
}
override fun onCancel(dialog: DialogInterface?) {
// Doesn't matter what you write here, the dialog will be closed.
super.onCancel(dialog)
}
override fun onDismiss(dialog: DialogInterface?) {
// Doesn't matter what you write here, the dialog will be closed.
super.onDismiss(dialog)
}