2012-01-31 70 views
2

我正在使用SDK附带的ActionBarCompat示例。我的活动过去有菜单项会根据活动的状态而改变。我会修改菜单,像这样在onPrepareOptionsMenu()如何在ICS运行时添加/删除菜单项?

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    menu.clear(); 

    if (some condition) { 
     menu.add(...); 
    } 
    ... 
} 

在ICS不过,我认为我们应该通过调用invalidateOptionsMenu(),然后我们onCreateOptionsMenu()再次呼吁修改菜单项。在这里,我们可以删除菜单项(不添加它们)。然后我们可能最终是这样的:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    if (SDK >= 11) { 
     // full menu is reloaded from xml every time we 
     // called invalidateOptionsMenu(), so we have a 
     // fresh menu here again. 
    } 
} 

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    if (SDK >= 11) { 
     // we can remove items depending on our state, 
     // but not add any, that's ok. 
    } 
    else { 
     // we only have our original menu instance here, 
     // so if we previously had called menu.remove(), 
     // we need to call menu.add() here: 
     if (foo == true) { 
      menu.remove(R.id.menu_item_foo); 
     } else { 
      menu.add(R.id.menu_item_foo, ...); 
     } 
    } 
} 

这似乎有点尴尬,

感谢

+0

只是确保,你叫'super.onPrepareOptionsMenu(。 )'最后,对吧? – 2012-01-31 23:21:21

+0

嗨,是的。看了这些之后,看起来在SDK 11+中我们可以调用invalidateOptionsMenu(),但在早期版本中我们没有相同的内容。所以想知道如何在保持代码一致的前提下在早期的平台上重建菜单。 – user291701 2012-02-01 10:29:59

+0

使用menu.clear并在需要时重建所需的菜单。 InvalidateOptionsMenu在那里,因为没有按下物理菜单键(当使用Actionbar模型时),所以需要知道何时更新。 – Kuffs 2012-02-01 11:19:10

回答

1

示范项目 https://github.com/AlienAsRoger/CourtDeadlines

这是我如何解决它:

在ActionBarHelperBase.java中的actionbarcompat项目

...

private View addActionItemCompatFromMenuItem(final MenuItem item) { 

final int itemId = item.getItemId(); 

....

对象的这类副本属性的创造者,但并未复制项目的ID,所以不可能与fiven后来发现它ID。

所以我增加了它在该方法中:

... 
actionButton.setId(itemId); 
... 

,并在同一个班,我只是用:

@Override 
public void hideMenuItemById(int id, boolean show){ 
    getActionBarCompat().findViewById(id).setVisibility(show? View.VISIBLE: View.GONE); 
} 

希望它可以帮助你。

更新:

在ActionBarActivity

从ActionBarCompat样本:

package com.chess.ui.activities; 

import actionbarcompat.ActionBarActivity; 
import actionbarcompat.ActionBarHelper; 
import android.app.ActionBar; 
import android.app.SearchManager; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.SearchView; 
import com.chess.R; 
import com.chess.lcc.android.LccHolder; 

public abstract class CoreActivityActionBar2 extends ActionBarActivity { 

    protected boolean showActionSearch; 
    protected boolean showActionSettings; 
    protected boolean showActionNewGame; 
    protected boolean showActionRefresh; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && getActionBar() != null) { 
      getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP 
        | ActionBar.DISPLAY_USE_LOGO 
        | ActionBar.DISPLAY_SHOW_HOME 
        | ActionBar.DISPLAY_SHOW_TITLE); 
     } 
    } 

    @Override 
    protected void onStart() { 
     if (HONEYCOMB_PLUS_API) { 
      adjustActionBar(); 
     } 
     super.onStart(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     if (!HONEYCOMB_PLUS_API) { 
      adjustActionBar(); 
     } 
    } 


    private void adjustActionBar() { 
     getActionBarHelper().showMenuItemById(R.id.menu_settings, showActionSettings); 
     getActionBarHelper().showMenuItemById(R.id.menu_new_game, showActionNewGame); 
     getActionBarHelper().showMenuItemById(R.id.menu_refresh, showActionRefresh); 
     getActionBarHelper().showMenuItemById(R.id.menu_search, showActionSearch); 
     getActionBarHelper().showMenuItemById(R.id.menu_singOut, LccHolder.getInstance(this).isConnected()); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater menuInflater = getMenuInflater(); 
     menuInflater.inflate(R.menu.sign_out, menu); 
     getActionBarHelper().showMenuItemById(R.id.menu_singOut, LccHolder.getInstance(this).isConnected(), menu); 
     getActionBarHelper().showMenuItemById(R.id.menu_search, showActionSearch, menu); 
     getActionBarHelper().showMenuItemById(R.id.menu_settings, showActionSettings, menu); 
     getActionBarHelper().showMenuItemById(R.id.menu_new_game, showActionNewGame, menu); 
     getActionBarHelper().showMenuItemById(R.id.menu_refresh, showActionRefresh, menu); 

     if (HONEYCOMB_PLUS_API) { 
      // Get the SearchView and set the searchable configuration 
      SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
      SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); 
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
     } 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       backToHomeActivity(); 
       break; 
      case R.id.menu_settings: 
       startActivity(new Intent(this, PreferencesScreenActivity.class)); 
       break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public ActionBarHelper provideActionBarHelper() { 
     return getActionBarHelper(); 
    } 

} 

其中showActionSettings是一个布尔标志切换知名度

+0

如何从我的活动中调用此方法? – tobias 2012-12-21 22:40:26

+0

看到更新后 – 2012-12-22 07:14:10

+0

我没有得到如何这应该工作。为什么只在HelperBase中添加showMenuById方法(这只是在Honeycomp之前)?您的代码不起作用,因为您不在HelperBase中调用该方法,而是在ActionBarHelper中调用该方法。 – tobias 2012-12-28 01:56:01