如何在 activity 的特定片段中禁用抽屉选项

How to disable drawer option in specific fragments of an activity

我有一个包含 5 个片段的 activity,我在我的 activity 中使用抽屉布局作为抽屉,但我只想在片段 2 中使用(启用)抽屉,我想禁用剩余片段中的抽屉选项。

任何人都可以帮助我如何做到这一点?

在您的 activity 中添加两种方法,一种禁用 drawer,另一种重新启用它,如下所示:

public void lockDrawer() {
    drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}


public void unlockDrawer() {
    drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}

然后在你的 fragments onCreateView(...) 方法中输入:

fragmentInteractionListener.lockDrawer();

对于 fragments,其中 drawer 应保持关闭状态,对于 fragments,其中 drawer 应保持打开状态:

fragmentInteractionListener.unlockDrawer();

P.S: 有关如何正确实现 fragment 交互侦听器的教程,请参阅:

https://developer.android.com/training/basics/fragments/communicating.html