简体   繁体   English

如何禁用底部导航视图中的选项卡?

[英]How to disable a tab in bottom navigation view?

I have 4 tabs in my app.我的应用中有 4 个标签。 In which one is accessible without log-in but others can't be.其中一个无需登录即可访问,但其他人则无法访问。 So, I need to implement a functionality in which if a user clicks on that tab then rest 4 will be disabled and when I click on those tabs I only want toast but if I click on those tabs I get toast but it also got selected but fragment not changed.因此,我需要实现一个功能,如果用户单击该选项卡,则 rest 4 将被禁用,当我单击这些选项卡时,我只想吐司,但如果我单击这些选项卡,我会吐司,但它也被选中了,但是片段没有改变。 I want to disable that tab I have assigned a value to a variable to check whether it is without logging or after logging.我想禁用那个我已经为变量赋值的选项卡,以检查它是没有记录还是在记录后。

Code:代码:

  case R.id.home:
                if(value.equals("1")){
                    Toast.makeText(CarerSeekerActivity.this,R.string.login_signup,Toast.LENGTH_SHORT).show();
                    navigation.getMenu().getItem(0).setEnabled(false);

                }
                else {
                    fragment = new CreatePersonalizedPackageCareSeekerFragment();
                    changeFragments(fragment);
                }
                return true;

but it is showing toast and but selected that tab.但它显示吐司,但选择了该选项卡。 I do not want to make it selected.我不想让它被选中。 Please help.请帮忙。

After setting the menu please write below code in your on create method: 设置菜单后,请在on创建方法中编写以下代码:

if(isloggedin){
// do click action which is required if the user already logged in
change your fragment from here
}else{
    bottomnavigation.getMenu().getItem(your_position).setEnabled(false); // disable menu if user not logged in
Toast.makeText(CarerSeekerActivity.this,R.string.login_signup,Toast.LENGTH_SHORT).show();
}

I know I'm replying 3 years later, but this is the function I've done for changing all the tabs to clickable or not clickable.我知道我会在 3 年后回复,但这是我所做的 function,用于将所有选项卡更改为可点击或不可点击。 You can easily adapt it for what you asked.您可以轻松地根据您的要求调整它。

private void setNavigationBarClickableItems(BottomNavigationBar bottomNavigationBar, boolean clickable) {
    BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationBar.getChildAt(0);

    for (int i = 0; i < menuView.getChildCount(); i++)  {
        menuView.getChildAt(i).setClickable(clickable);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM