android - 使用 Android 导航组件隐藏抽屉项目
问题描述
我正在使用导航组件和导航抽屉。如果用户使用 FirebaseAuth 登录,LoginFragment
我想从导航抽屉中隐藏登录/注册菜单。谁能告诉我我该怎么做?
解决方案
如果用户在我的 LoginFragment 中使用 FirebaseAuth 登录,我想从导航抽屉中隐藏登录/注册菜单
您可以从NavigationView
附加到导航抽屉的菜单中获取菜单并用于removeItem(id)
删除特定项目:
if (userLoggedIn) { // Set that to your logic when the user logged in.
removeItem(R.id.singup); // adjust that to the id you set to sign up
removeItem(R.id.login); // adjust that to the id you set to login
}
public removeItem(int id) {
NavigationView navView = findViewById(R.id.foo); // Add your NavigationView id
Menu menu = navView.getMenu();
if (menu.findItem(id) != null) // Make sure that the item exists in the menu
menu.removeItem(id);
}
更新
导航抽屉在我的 MainActivity
要从片段中访问活动,请使用requireActivity()
或getActivity()
类似:
((MainActivity) requireActivity()).removeItem(R.id.login); // adjust that to the id you set to sign up
((MainActivity) requireActivity()).removeItem(R.id.signup); // adjust that to the id you set to login
推荐阅读
- python - 错误:计算多项式时出现“预期单个张量时的张量列表”
- python - 如何以与 python 控制台相同的格式打印
- mysql - 具有已定义值集的 TEXT 列上的索引
- android-studio - 如何使用 CardView 打开链接
- r - as.character(x$content) 中的错误:无法将“闭包”类型强制转换为“字符”类型的向量
- php - 我在 PHP 5.6 中使用 PHP 7.1 中的 [] 创建了数组,给出了致命错误
- knockout.js - 调试时如何从可观察对象中查找所有订阅函数
- python - 递归程序打印但没有返回正确的值
- python - ModuleNotFoundError:pip 安装后没有名为“请求”的模块
- javascript - Can't import d3-queue in React.js? Attempted import error: 'queue' is not exported from 'd3' (imported as 'd3')