我怎样才能刷新动作条上时,prepareOptionsMenu切换菜单项? [英] How can I refresh the ActionBar when onPrepareOptionsMenu switched menu entries?

查看:158
本文介绍了我怎样才能刷新动作条上时,prepareOptionsMenu切换菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我经常启用/禁用菜单项,也使他们从prepareOptionsMenu可见。

Within my apps I often enable/disable menu entries and do make them visible from onPrepareOptionsMenu.

今天我开始了Android添加:showAsAction菜单属性一些我的Andr​​oid 2.x的应用程序,以显示菜单项中使用的大多数的动作条

Today I started to add the android:showAsAction menu attribute to some of my Android 2.x apps to show menu entries used most on the ActionBar.

在动作条不反映启用/禁用和知名度马上。我需要点击菜单下拉列表右侧看到这种变化的发生。

The ActionBar does not reflect the enable/disable and visibility immediately. I need to click on the menu dropdown on the right to see this change happen.

好吧,我不明白,在prepareOptionsMenu菜单火灾。但是,我需要做的刷新动作条?我想,这种变化需要从内onOptionsItemSelected应用,但我不知道我应该叫。

Ok, I do understand that the menu fires onPrepareOptionsMenu. But what do I need to do to refresh the ActionBar? I think this change needs to be applied from within onOptionsItemSelected but I don't know what I should call.

这是菜单:

<item
    android:icon="@drawable/ic_menu_mapmode"
    android:id="@+id/men_mapview"
    android:showAsAction="ifRoom|withText"
    android:title="@string/txt_mapview" />

<item
    android:icon="@drawable/ic_menu_mapmode"
    android:id="@+id/men_satelliteview"
    android:showAsAction="ifRoom|withText"
    android:title="@string/txt_satelliteview" />

下面是上prepareOptionsMenu:

Here's the onPrepareOptionsMenu:

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
    MenuItem menuItemMapView = menu.findItem(R.id.men_mapview);
    MenuItem menuItemSatelliteView = menu.findItem(R.id.men_satelliteview);

    if (mapView.isSatellite()) {
        menuItemMapView.setEnabled(true).setVisible(true);
        menuItemmenuItemSatelliteView.setEnabled(false).setVisible(false);
    } else {
        menuItemMapView.setEnabled(false).setVisible(false);
        menuItemmenuItemSatelliteView.setEnabled(true).setVisible(true);
    }

    return super.onPrepareOptionsMenu(menu);
}

这里的onOptionsItemSelected

Here's the onOptionsItemSelected

@Override
public boolean onOptionsItemSelected(final MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case R.id.men_mapview:
            mapView.setSatellite(false);
            mapView.setStreetView(true);
            mapView.invalidate();

            invalidateOptionsMenu(); // This works on Android 3.x devices only
            return true;
        case R.id.men_satelliteview:
            mapView.setSatellite(true);
            mapView.setStreetView(false);
            mapView.invalidate();

            invalidateOptionsMenu(); // This works on Android 3.x devices only
            return true;
    }

    return super.onOptionsItemSelected(menuItem);
}

编辑::如果我加入invalidateOptionsMenu这部作品在Android 3.x的应用程序,但崩溃,因为一名失踪法的Andr​​oid 2.x的设备。什么是推荐的方式做到这一点吧?

If I add invalidateOptionsMenu this works on Android 3.x apps but crashes on Android 2.x devices because of a missing method. What's the recommended way to do it right?

推荐答案

我所选择的方法是创建一个辅助类。例如:

My method of choice is to create a helper class. For example:

class VersionHelper
{
    static void refreshActionBarMenu(Activity activity)
    {
        activity.invalidateOptionsMenu();
    }
}

现在在code以上,替换 invalidateOptionsMenu();

Now in your code above, replace invalidateOptionsMenu(); with:

if (Build.VERSION.SDK_INT >= 11)
{
    VersionHelper.refreshActionBarMenu(this);
}

信贷这个方法去CommonsWare(搜索HoneycombHelper,并检查了他的书 - 强烈推荐)

Credit for this method goes to CommonsWare (search for HoneycombHelper, and check out his books - highly recommended)

这篇关于我怎样才能刷新动作条上时,prepareOptionsMenu切换菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆