android - 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>
解决方案
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.
推荐阅读
- java - 使用 0 作为主键
- python-3.x - Python 3 一起收集表格
- composer-php - 未定义的属性:Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository::$verbos
- vhdl - 两个连续的信号分配(一个有延迟)如何在 VHDL 中工作
- javascript - 如何在所有 jsamine 测试之前运行脚本?
- ios - UIView pan 手势与系统手势冲突
- c# - 缺少刚体字段的引用异常和?? 操作员
- android - ScaleM() 方法更改 OpenGL ES 2.0/3.0 中的着色器效果
- javascript - TradingView 轻量级图表价格似乎无法正确缩放
- assembly - 汇编程序 GNU:小写到大写,反之亦然