Android ActionBar Action Items

时间:2012-12-07 09:22:50

标签: android android-actionbar actionbarsherlock

我正在使用Actionbar Sherlock并且我有三个按钮

  1. 使用SearchView搜索
  2. 打开Dialog Fragment的类别
  3. 打开隐藏下拉菜单的排序
  4. 当我单击“搜索”按钮时,SearchView文本会展开。当搜索视图已展开时,我想隐藏操作栏中的所有其他图标,当我退出展开的searchView模式时它应该返回。

    public class MainActivity extends SherlockFragmentActivity implements
            SearchView.OnQueryTextListener {
        protected static CharSequence[] _categories = { "Amusement Park",
                "Bird Sanctuary", "Wild Life", "River", "Hill Station", "Temple" };
    
        protected static boolean[] _selections = new boolean[_categories.length];
        public SearchView mSearchView;
        private TextView mStatusView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            MenuInflater inflater = getSupportMenuInflater();
            inflater.inflate(R.menu.activity_main, menu);
    
            MenuItem searchItem = menu.findItem(R.id.action_search);
            MenuItem categoryItem = menu.findItem(R.id.action_category);
            MenuItem sortItem = menu.findItem(R.id.action_sort);
            mSearchView = (SearchView) searchItem.getActionView();
            setupSearchView(searchItem, categoryItem, sortItem);
            return true;
        }
    
        public boolean onOptionsItemSelected(MenuItem item) {
    
            switch (item.getItemId()) {
            /*
             * case R.id.action_go: Intent i = new Intent(MainActivity.this,
             * PlaceActivity.class); startActivity(i); break;
             */
            /*
             * case R.id.action_search: Toast.makeText(this, "Searh",
             * Toast.LENGTH_LONG).show(); break;
             */
            case R.id.action_category:
                showDialog();
                break;
            case R.id.action_sort_alpha_az:
                Toast.makeText(this, "Alpha AZ", Toast.LENGTH_LONG).show();
                break;
            case R.id.action_sort_alpha_za:
                Toast.makeText(this, "Alpha ZA", Toast.LENGTH_LONG).show();
                break;
            case R.id.action_sort_dist_nf:
                Toast.makeText(this, "Dist NF", Toast.LENGTH_LONG).show();
                break;
            case R.id.action_sort_dist_fn:
                Toast.makeText(this, "Dist FN", Toast.LENGTH_LONG).show();
                break;
            default:
                // return super.onOptionsItemSelected(item);
                break;
            }
            return true;
        }
    
        private void setupSearchView(MenuItem searchItem, MenuItem categoryItem,
                MenuItem sortItem) {
            mSearchView.setIconifiedByDefault(true);
            searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS
                    | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        }
    
        public boolean onQueryTextChange(String newText) {
            Log.i("Nomad", "onQueryTextChange");
            return false;
        }
    
        public boolean onQueryTextSubmit(String query) {
            Log.i("Nomad", "onQueryTextSubmit");
            return false;
        }
    
        public boolean onClose() {
            Log.i("Nomad", "onClose");
            return false;
        }
    
        protected boolean isAlwaysExpanded() {
            return false;
        }
    
    
    }
    

3 个答案:

答案 0 :(得分:8)

我的做法如下:

private Menu mainMenu = null;

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.activity_main, menu);

    mainMenu = menu;

    MenuItem searchItem = menu.findItem(R.id.action_search);
    MenuItem categoryItem = menu.findItem(R.id.action_category);
    MenuItem sortItem = menu.findItem(R.id.action_sort);
    mSearchView = (SearchView) searchItem.getActionView();
    mSearchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) { 
                //hide  action item
                if (mainMenu != null)   
                    mainMenu.findItem(R.id.quick_actions).setVisible(false);
        }
    });
    mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {           
        @Override
        public boolean onClose() {
            //re-show the  action button 
                if (mainMenu != null)
                    mainMenu.findItem(R.id.quick_actions).setVisible(true);
            return false;
        }
    });
    //setupSearchView(searchItem, categoryItem, sortItem);
    return true;
}

答案 1 :(得分:0)

我的解决方案是:

@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater)
{
    final MenuItem item = menu.add(0, MENU_SEARCH, 0, "Search");

    final SearchView searchView = new SearchView(getActivity());

    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() 
    {
        @Override
        public void onFocusChange(View view, boolean queryTextFocused) 
        {
            if(!queryTextFocused) 
            {
                item.collapseActionView();
            }

            int count = menu.size();

            for (int i = 0; i < count; i ++)
            {
                MenuItem it = menu.getItem(i);
                it.setVisible(item.equals(it)) || !queryTextFocused);
            }
        }
    });

    item.setActionView(searchView);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}

答案 2 :(得分:0)

只需在菜单的xml上,只需将属性指定为app:showAsAction =&#34; collapseActionView | ifRoom&#34;对于seachview和其他菜单项,请将其作为app:showAsAction =&#34; ifRoom&#34;。这将为您提供预期的结果。