有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android导航组件,带有多个菜单,用于底部导航

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

其想法是启动启动屏幕,检查用户是否登录。如果用户已登录,则会加载主屏幕,并显示底部导航(以前隐藏)

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

然而,当用户登录时,它可以是两种类型的用户之一。如果他是用户,他可以访问应用程序的所有部分。然而,如果用户是访问者,他将只能访问两个部分。在这种情况下,我想有一个不同的底部导航菜单,只有2个标签可见

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

这是主活动中的代码:

    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中的代码(这是导航图中的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);
}

共 (1) 个答案

  1. # 1 楼答案

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

    如果用户未订阅,则可以使用removeItem()以编程方式从菜单中删除所需的项

    因此,在MainActivity中添加一个条件:

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

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