Android - 具有用于底部导航的多个菜单的导航组件 [英] Android - Navigation Component with multiple menus for Bottom Navigation

查看:22
本文介绍了Android - 具有用于底部导航的多个菜单的导航组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Android 应用程序 (Java),它使用导航组件来设置底部导航.该应用由一个活动(主)组成,其他所有内容都以片段形式加载.

I have an Android app (Java) which uses the Navigation Component to set up a Bottom Navigation. The app consists of a single Activity (Main), everything else is loaded in fragments.

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

The idea is for the Splash Screen to launch and check if the user is logged in or not. If the user is logged in, then the home screen loads and the bottom navigation (previously hidden) becomes visible.

底部导航由 3 个标签组成.

The bottom navigation consists of 3 tabs.

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

However, when the user signs in, it can be one of two types of users. If he's a subscriber he will get access to all sections of the app. However if the user is a visitor, he will get access to only two sections. In this case I want to have a different bottom navigation menu to be visible with only 2 tabs.

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

How can I replace the bottom navigation depending on the user type if the main bottom navigation has already been assigned in the main 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):

This is the code in the SplashScreenFragment (this is the startDestination in the nav_graph):

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 中可以有完整的菜单项,所以当订阅的用户登录时,在菜单是必需的.

You can have the full menu items by default in the BottomNavigationView, so when a subscribed user logged in, then no change in the menu is required.

如果用户没有订阅,那么您可以使用 removeItem()

And if the user is not subscribed, then you can remove the needed items from the menu programmatically using removeItem()

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

So, in MainActivity add a condition :

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

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

And do the same for all menu item ids that you want to remove

这篇关于Android - 具有用于底部导航的多个菜单的导航组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆