0

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> 
4

3 回答 3

0

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.

于 2019-01-15T13:50:42.710 回答
0

Seems like this is an issue with Android or the Bottom Navigation View. When I executed the reselection of the previous fragment after a small delay of 50milliseconds its working fine. ie the reselected fragment or the previous fragments icon gets highlighted as required.

if (args.Item.ItemId == Resource.Id.menu_downloads)
            {
                List<Product> _downloadProducts = DBService.GetDB().GetDownloadedProducts();
                if (_downloadProducts == null || _downloadProducts.Count == 0)
                {

                   _readProgressTimerTask = new Timer
                    {
                        Enabled = true,
                        Interval = 50,
                        AutoReset = false
                    };
                    _readProgressTimerTask.Elapsed += OnProgressCheckTimeElapsed;
                    Toast.MakeText(this, this.Resources.GetString(Resource.String.no_downloads), ToastLength.Short).Show();
                }
                else
                {
                    _downloadGalleryFragment = new DownloadGalleryFragment(_downloadProducts);
                    fragment = _downloadGalleryFragment;
                    _fragmentTag = "Downloads";
                }
            }

private void OnProgressCheckTimeElapsed(System.Object source, ElapsedEventArgs args)
        {
            this.RunOnUiThread(() =>
            {
                _bottomNavigationView.SelectedItemId = _selectedToolbarId;
            });
        }
于 2019-01-18T19:21:18.227 回答
0

I recommend that you first read the guideline available here to implement bottom navigation for android.

That what you want doesn't make much sense, an empty screen should be shown saying something like "There is not content available" or the message you like.

In this link BottomNavigationView you can find all public methods and listeners you can set to bottom navigation. As for your case, you might be interested in adding these two.

setOnNavigationItemReselectedListener()

setOnNavigationItemSelectedListener()

Best regards, Pedro.

于 2019-01-18T19:43:54.927 回答