BottomNavigationView 的 android 中是否有 shouldSelectViewController 的等效项?

Is there an equivalent for shouldSelectViewController in android for BottomNavigationView?

我的 android activity 中有一个 BottomNavigationView,它由 4 个菜单项组成。当我点击下载菜单时,我会检查是否有任何可用的下载内容,如果可用,我将允许导航发生,如果没有下载的内容,我将显示一个 Toast 说明相同的内容,我希望上一个选项卡保持选中状态.在 iOS 中,我可以使用委托方法 shouldSelectViewController 来确定是否允许导航。 方法签名指定如下:

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

我尝试重新选择之前选择的选项卡,结果保留了之前的项目,但选择的项目颜色仍然分配给下载选项卡。

    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> 

没有像 shouldSelectViewController 这样的委托,但您可以获取底部导航栏的菜单项并禁用这些菜单项:

像这样:

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

一旦你有了它们,你就可以随心所欲地操作它们,要启用或禁用一个项目,只需使用将 boolean 作为参数的 SetEnabled 方法。

这似乎是 Android 或底部导航视图的问题。当我在 50 毫秒的小延迟后执行前一个片段的重新选择时,它工作正常。即重新选择的片段或以前的片段图标会根据需要突出显示。

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;
            });
        }

我建议您先阅读指南 available here 以实现 android 的底部导航

您想要的内容没有多大意义,应该显示一个空白屏幕,上面写着 "There is not content available" 之类的内容或您喜欢的消息。

在此 link BottomNavigationView 中,您可以找到所有 public 方法和可以设置为底部导航的侦听器。至于你的情况,你可能有兴趣添加这两个。

setOnNavigationItemReselectedListener()

setOnNavigationItemSelectedListener()

此致,佩德罗。