问题描述
我有一个具有2段的活动。两者都是ListFragments两者的菜单贡献的MenuItems。我有我设置属性的android 1菜单项:showAsAction把它作为展示的动作条的按钮。这工作得很好。
I have an Activity that has 2 fragments. Both are ListFragments and both contribute MenuItems to the Menu. I have one MenuItem that I've set the attribute android:showAsAction to have it show as a button on the ActionBar. Which works fine.
现在的菜单项的状态有关。这对暂停和恢复队列中暂停/恢复菜单选项。我的问题是我不知道如何设置它的创建片段时,最初的雕像。
Now the MenuItem is state dependent. It's a Pause/Resume menu option for pausing and resuming a queue. My problem is I can't figure out how to set it's initial statue when the Fragment is created.
它的状态取决于队列是否暂停或不。所以,我有获取队列,并设置一个布尔(暂停),基于队列的状态的AsyncTask的。我呼吁prepareOptionsMenu设置文本的基础上队列的最后一个已知的状态,暂停菜单项来完成这项工程,如果我离开这个菜单项,在实际的菜单。你点击菜单图标和prepareOptionsMenu被触发,它会显示之前的菜单更新。
It's state is dependent on the whether the queue is paused or not. So I have an AsyncTask that gets the queue and sets a boolean (paused) based on the state of the queue. I'm calling onPrepareOptionsMenu to set the text for the Pause menu item based on the last known state of the queue and this works great if I leave the MenuItem in the actual menu. You tap the menu icon and onPrepareOptionsMenu is fired and the menu is updated before it's displayed.
现在的问题是,如果我把同样的菜单项上的动作条(showAsAction),我怎么能强迫它,而不必在prepareOptionsMenu来电更新?我需要能够做到这一点,因为在第一次推出的应用程序,我发送一个请求,以获得队列,但动作条以后的任务返回的是设置和显示。我创建了我的片段的处理程序被调用每次我得到一个队列的更新时间,但是从那里,我怎么可以更新的文字我的菜单项上的动作条?我似乎无法找到一个方法来获得当前设置的菜单,除了在prepareOptionMenu操纵它。
The problem is, if I put that same MenuItem on the ActionBar (showAsAction), how can I force it to update without having to call onPrepareOptionsMenu? I need to be able to do this because on first launch of the app, I send a request to get the queue, but the task returns after the ActionBar is setup and displayed. I've created a handler in my fragment that gets called every time I get a queue update, but from there, how can I update the text for my MenuItem on the ActionBar? I can't seem to find a way to get the currently set Menu to manipulate it except for in onPrepareOptionMenu.
罗布W的
推荐答案
选项#1:尝试<$c$c>invalidateOptionsMenu()$c$c>.我不知道这是否会迫使操作栏立即重绘与否。
Option #1: Try invalidateOptionsMenu()
. I don't know if this will force an immediate redraw of the action bar or not.
选项2:看看 getActionView()
返回任何受影响菜单项
。这可能是 showAsAction
只是自动为您创建行动的看法。如果是这样,你可以presumably启用/禁用查看
。
Option #2: See if getActionView()
returns anything for the affected MenuItem
. It is possible that showAsAction
simply automatically creates action views for you. If so, you can presumably enable/disable that View
.
我似乎无法找到一个方法来获得当前设置的菜单来操作它,除了在prepareOptionMenu。
您可以挂到菜单
反对你交了 onCreateOptionsMenu()
。引用的docs:
You can hang onto the Menu
object you were handed in onCreateOptionsMenu()
. Quoting the docs:
您可以安全地保存在菜单(从它创建的任何项目),根据需要进行修改时,在下一次onCreateOptionsMenu()被调用到。
这篇关于如何更新的动作条显示的菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!