首页 > 解决方案 > Is there an equivalent for shouldSelectViewController in android for BottomNavigationView?

问题描述

I have a BottomNavigationView in my android activity, which consists of 4 menuItem. When I tap on the downloads menu I check if there are any downloaded contents available, if available I will allow the navigation to happen and if there is no downloaded content I will show a Toast stating the same and I want the previous tab to remain selected. In iOS I can use the delegate method shouldSelectViewController to determine whether navigation can be allowed or not. The method signature is specified below:

- (BOOL)tabBarController:(UITabBarController *)tabBarController 
shouldSelectViewController:(UIViewController *)viewController;

I tried reselecting the previously selected tab, as a result, the previous item is retained but the selected item color is still assigned to the downloads tab.

    private void BottomNavigationItemSelected(object obj, BottomNavigationView.NavigationItemSelectedEventArgs args)
    {
        Android.Support.V4.App.Fragment fragment = null;
        Android.Support.V4.App.Fragment currentFragment = SupportFragmentManager.FindFragmentById(Resource.Id.content_frame);
        string title = "";
        if (args.Item.ItemId == Resource.Id.menu_explore)
        {
            _selectedToolbarId = args.Item.ItemId;
            title = Resources.GetString(Resource.String.shelf_title);
            fragment = _exploreFragment;
            _fragmentTag = "Home";
        }
        else
        {
            title = args.Item.TitleFormatted.ToString();
        }
        if (args.Item.ItemId == Resource.Id.menu_dashboard)
        {
            //COULD BE MADE CONFIGURABLE
            //fragment = _dashboardFragment;
            _selectedToolbarId = args.Item.ItemId;
            fragment = _redesignDashboard;
            _fragmentTag = "Dashboard";
        }
        else if (args.Item.ItemId == Resource.Id.menu_more)
        {
            _selectedToolbarId = args.Item.ItemId;
            fragment = _moreFragment;
            _fragmentTag = "More";
        }
        else if (args.Item.ItemId == Resource.Id.menu_report)
        {
            _selectedToolbarId = args.Item.ItemId;
            fragment = _reportFragment;
            _fragmentTag = "Report";
        }
        else if (args.Item.ItemId == Resource.Id.menu_downloads)
        {
            List<Product> _downloadProducts = DBService.GetDB().GetDownloadedProducts();
            if (_downloadProducts == null || _downloadProducts.Count == 0)
            {
                _bottomNavigationView.SelectedItemId = _selectedToolbarId;
                Toast.MakeText(this, "No downloaded products", ToastLength.Short).Show();
                args.Item.SetChecked(false);
            }
            else
            {
                _downloadGalleryFragment = new DownloadGalleryFragment(_downloadProducts);
                fragment = _downloadGalleryFragment;
                _fragmentTag = "Downloads";
            }
        }
        if (fragment != null)
        {
            _toolbarTitle.Text = title;
            ToggleTitle(true);
            SupportFragmentManager.BeginTransaction().SetCustomAnimations(Resource.Animation.fab_slide_in_from_right, Resource.Animation.fab_slide_out_to_left).Replace(Resource.Id.content_frame, fragment, _fragmentTag).Commit();
        }
    }

<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
      android:id="@+id/menu_explore"
      android:enabled="true"
      android:title="@string/explore"
      android:icon="@drawable/explore_icon"
      app:showAsAction="always" />

    <item
      android:id="@+id/menu_dashboard"
      android:enabled="true"
      android:title="@string/dashboard"
      android:icon="@drawable/Dashboard_new_icon"
      app:showAsAction="always" />

     <item
      android:id="@+id/menu_report"
      android:enabled="true"
      android:title="@string/reports"
      android:icon="@drawable/dashboard_icon"
      app:showAsAction="always" />

     <item
      android:id="@+id/menu_downloads"
      android:enabled="true"
      android:title="@string/menu_downloads"
      android:icon="@drawable/download_icon"
      app:showAsAction="always" />

     <item
      android:id="@+id/menu_more"
      android:enabled="true"
      android:title="@string/more_bottombar"
      android:icon="@drawable/more_icon"
      app:showAsAction="always" />
</menu> 

标签: androidiosxamarin.iosxamarin.androidbottomnavigationview

解决方案


There is no such delegate that works as shouldSelectViewController but what you can do is get the menu items of the bottom nav bar and disables these menu items:

Something like this:

var listMenuItems = new List<IMenuItem>();
for (int i = 0; i < bottomNav.Menu.Size(); i++)
{
   listMenuItems.Add(bottomNav.Menu.GetItem(i));
}

Once you have them here you can manipulate them as you like, to enable or disable an item just use the SetEnabled method that takes a boolean as a parameter.


推荐阅读