2

I am switching from Sherlock to AppCompat, and I am used to do this thing where I replace refresh action item with a progress bar while something is loading like this

public void setRefreshButtonState(boolean refreshing) {
    if (mOptionsMenu == null) {
        return;
    }

    final MenuItem refreshItem = mOptionsMenu.findItem(R.id.action_refresh);
    if (refreshItem != null) {
        if (refreshing) {
            refreshItem.setActionView(R.layout.actionbar_indeterminate_progress);
        } else {
            refreshItem.setActionView(null);
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    mOptionsMenu = menu;
    return super.onCreateOptionsMenu(menu);
}

But since Menu is the regular menu in AppCompat (unlike Sherlock), it obviously throws

Call requires API level 11 (current min is 9): android.view.MenuItem#setActionView

I tried all the MenuItemCompat static methods, but no luck

Thanks!

4

2 回答 2

2

我在寻找错误的东西。你不想comapt菜单对象而是菜单项,像这样

MenuItemCompat.setActionView(refreshItem, R.layout.actionbar_indeterminate_progress);

于 2013-10-16T23:23:21.837 回答
0
public void setActionItemInProgress(MenuItem menuItem, boolean refreshing) {
    if (refreshing) {
        MenuItemCompat.setActionView(menuItem, R.layout.actionbar_indeterminate_progress);
    } else {
        MenuItemCompat.setActionView(menuItem, null);
    }
}
于 2013-12-19T10:19:24.523 回答