使背景片段不可交互
Make background fragment not interactable
我目前正在通过来自另一个片段的 add()
方法向我的 supportFragmentManager
添加一个新片段(使用单个 activity 模式)。但是我的新片段只使用了一半的屏幕,我仍然可以在后台与片段进行交互。有什么方法可以阻止用户与背景片段的交互吗?就像 DialogFragment 一样。另外,由于 this.
,我无法使用对话框片段
好吧,在发布问题后,我在 DialogFragment 代码中查找并在 onStart()
方法中找到了这一行:
View decorView = mDialog.getWindow().getDecorView();
ViewTreeLifecycleOwner.set(decorView, this);
ViewTreeViewModelStoreOwner.set(decorView, this);
ViewTreeSavedStateRegistryOwner.set(decorView, this);
为了让它在常规片段中工作,我刚刚编辑了第一行
val view = myRootView as View //View decorView = mDialog.getWindow().getDecorView();
就是这样
您可以使用一半屏幕的对话框片段(您是否使用了 material 对话框片段)。
https://medium.com/@kosta.palash/using-bottomsheetdialogfragment-with-material-design-guideline-f9814c39b9fc
并且您可以使用 Eventbus(https://github.com/greenrobot/EventBus) or SingleEvent (https://abhiappmobiledeveloper.medium.com/android-singleliveevent-of-livedata-for-ui-event-35d0c58512da#:~:text=LiveData%20notifies%20Observer%20objects%20when,every%20time%20there's%20a%20change.) 来通知后台片段
我目前正在通过来自另一个片段的 add()
方法向我的 supportFragmentManager
添加一个新片段(使用单个 activity 模式)。但是我的新片段只使用了一半的屏幕,我仍然可以在后台与片段进行交互。有什么方法可以阻止用户与背景片段的交互吗?就像 DialogFragment 一样。另外,由于 this.
好吧,在发布问题后,我在 DialogFragment 代码中查找并在 onStart()
方法中找到了这一行:
View decorView = mDialog.getWindow().getDecorView();
ViewTreeLifecycleOwner.set(decorView, this);
ViewTreeViewModelStoreOwner.set(decorView, this);
ViewTreeSavedStateRegistryOwner.set(decorView, this);
为了让它在常规片段中工作,我刚刚编辑了第一行
val view = myRootView as View //View decorView = mDialog.getWindow().getDecorView();
就是这样
您可以使用一半屏幕的对话框片段(您是否使用了 material 对话框片段)。 https://medium.com/@kosta.palash/using-bottomsheetdialogfragment-with-material-design-guideline-f9814c39b9fc
并且您可以使用 Eventbus(https://github.com/greenrobot/EventBus) or SingleEvent (https://abhiappmobiledeveloper.medium.com/android-singleliveevent-of-livedata-for-ui-event-35d0c58512da#:~:text=LiveData%20notifies%20Observer%20objects%20when,every%20time%20there's%20a%20change.) 来通知后台片段