[英]Add an item in bottom navigation bar
I am trying to add an item in a bottom menu navigation bar according to a var got from an API.我正在尝试根据从 API 获得的变量在底部菜单导航栏中添加一个项目。
I am able to delete an item from this navigation bar, like this :我可以从此导航栏中删除一个项目,如下所示:
if (restaurant.acceptsBookings == false) {
bottom_navigation_view.menu.removeItem(R.id.bottom_menu_book)
}
The problem is, when I am launching my app, we can see the icon during like, half of a second, then it disappears.问题是,当我启动我的应用程序时,我们可以在半秒内看到该图标,然后它消失了。
This is not that bad, but I was hoping there is a better and neater way to do this ;这还不错,但我希望有一种更好、更整洁的方法来做到这一点; for example by adding the elements in an empty navigation bar, instead of removing them.例如,通过在空导航栏中添加元素,而不是删除它们。
Here is my navigation bar xml code :这是我的导航栏 xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/bottom_menu_home"
android:enabled="true"
android:title="@string/bottom_menu_home"
android:icon="@drawable/ic_home"
app:showAsAction="ifRoom" />
<item
android:id="@+id/bottom_menu_menu"
android:enabled="true"
android:icon="@drawable/ic_menu"
android:title="@string/bottom_menu_menu"
app:showAsAction="ifRoom" />
<item
android:id="@+id/bottom_menu_profile"
android:enabled="true"
android:title="@string/bottom_menu_profile"
android:icon="@drawable/ic_user"
app:showAsAction="ifRoom" />
<item
android:id="@+id/bottom_menu_book"
android:enabled="true"
android:icon="@android:drawable/ic_menu_my_calendar"
android:title="@string/bottom_menu_bookings"
app:showAsAction="ifRoom" />
<item
android:id="@+id/bottom_menu_fidelity"
android:enabled="true"
android:icon="@drawable/giftgrey2"
android:title="@string/bottom_menu_fidelity"
app:showAsAction="ifRoom" />
</menu>
Do someone have a solution for this ?有人对此有解决方案吗?
Thanks in advance.提前致谢。
First, thanks everyone for helping me.首先感谢大家对我的帮助。
I tried both methods and they work perfectly : Hide the navigation bar with我尝试了这两种方法,它们工作得很好:隐藏导航栏
bottomNavigationMenu?.visibility = View.GONE
just after bottomNavigationMenu declaration, and then set就在bottomNavigationMenu声明之后,然后设置
bottomNavigationMenu?.visibility = View.VISIBLE
just after API response.就在 API 响应之后。
The method which create dynamically an item works too, here is how动态创建项目的方法也有效,这里是如何
bottomNavigationMenu?.menu?.add(Menu.NONE, 1, Menu.NONE, "TEST")?.setIcon(R.drawable.ic_home)
Here is what the man tell about the add fun (from https://developer.android.com/reference/android/view/Menu.html ; ctrl + f "add" 43) :以下是该男子讲述的添加乐趣(来自https://developer.android.com/reference/android/view/Menu.html ; ctrl + f "add" 43):
groupId int: The group identifier that this item should be part of. groupId int:该项目应该属于的组标识符。 This can be used to define groups of items for batch state changes.这可用于定义批次状态更改的项目组。 Normally use NONE if an item should not be in a group.如果一个项目不应该在一个组中,通常使用 NONE。
itemId int: Unique item ID. itemId int:唯一的项目 ID。 Use NONE if you do not need a unique ID.如果不需要唯一 ID,请使用 NONE。
order int: The order for the item. order int:商品的订单。 Use NONE if you do not care about the order.如果您不关心订单,请使用 NONE。 See getOrder().请参见 getOrder()。
title CharSequence: The text to display for the item. title CharSequence:要为项目显示的文本。
Thanks everyone for helping !感谢大家的帮助!
This sounds like a race condition.这听起来像是一种竞争条件。
What you can do is block the drawing of the navigation menu or the entire screen until after you get a response from the API.您可以做的是阻止绘制导航菜单或整个屏幕,直到您从 API 收到响应。 This just means your app will take half a second longer to launch.这只是意味着您的应用程序将需要半秒的时间才能启动。
Instead of using a static menu.xml file, what you can do is dynamically add menus that suits your condition.您可以做的是动态添加适合您的条件的菜单,而不是使用静态 menu.xml 文件。
Perhaps, this link may help: Inflate Bottom Navigation View menu programmatically也许,此链接可能会有所帮助:以编程方式充气底部导航视图菜单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.