Android - 具有多个底部导航菜单的导航组件

Android - Navigation Component with multiple menus for Bottom Navigation

我有一个 Android 应用程序 (Java),它使用导航组件来设置底部导航。该应用程序包含一个 Activity(主要),其他所有内容都以片段形式加载。

这个想法是让启动画面启动并检查用户是否登录。如果用户已登录,则会加载主屏幕并且底部导航(之前隐藏)变为可见。

底部导航由 3 个选项卡组成。

但是,当用户登录时,它可以是两种类型的用户之一。如果他是订阅者,他将可以访问该应用程序的所有部分。但是,如果用户是访客,他将只能访问两个部分。在这种情况下,我希望只有 2 个选项卡才能看到不同的底部导航菜单。

如果主底部导航已经在主activity中分配,如何根据用户类型替换底部导航?

这是MainActivity中的代码:

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    FragmentManager supportFragmentManager = getSupportFragmentManager();
    NavHostFragment navHostFragment = (NavHostFragment) supportFragmentManager.findFragmentById(R.id.nav_host_fragment);
    NavController navController = navHostFragment.getNavController();

    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.homeFragment, R.id.libraryFragment, R.id.searchFragment, R.id.myStuffFragment).build();
    NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);

    BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
    NavigationUI.setupWithNavController(bottomNav, navController);


    navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
        @Override
        public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
            if (destination.getId() == R.id.splashScreenFragment || destination.getId() == R.id.welcomeFragment || destination.getId() == R.id.signInFragment) {
                toolbar.setVisibility(View.GONE);
                bottomNav.setVisibility(View.GONE);
            } else if (destination.getId() == R.id.audioPlayerFragment) {
                bottomNav.setVisibility(View.GONE);
            } else {
                toolbar.setVisibility(View.VISIBLE);
                bottomNav.setVisibility(View.VISIBLE);
            }
        }
    });

这是 SplashScreenFragment 中的代码(这是 nav_graph 中的 startDestination):

public class SplashScreenFragment extends Fragment {

private static final String TAG = "SplashScreenFragment";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_splash_screen, container, false);

    return v;
}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    SharedPreferences getSharedData = this.getActivity().getSharedPreferences("AppTitle", Context.MODE_PRIVATE);
    Boolean loggedIn = getSharedData.getBoolean("isUserLoggedIn", false);


    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            if (!loggedIn) {
                Navigation.findNavController(view).navigate(R.id.action_splashScreenFragment_to_welcomeFragment);
            } else {
                Navigation.findNavController(view).navigate(R.id.action_splashScreenFragment_to_homeFragment);
            }
        }
    }, 2000);
}

默认情况下,您可以在 BottomNavigationView 中拥有完整的菜单项,因此当订阅用户登录时,不需要更改菜单。

如果用户未订阅,则您可以使用 removeItem()

以编程方式从菜单中删除所需的项目

所以,在MainActivity中添加一个条件:

if (notSubscribedUser) {
    if (bottomNav.getMenu().findItem(R.id.my_item_id) != null)
        bottomNavigationView.getMenu().removeItem(R.id.my_item_id);
}

并对您要删除的所有菜单项 ID 执行相同操作