2

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

  1. 使用 SearchView 进行搜索
  2. 打开对话框片段的类别
  3. 排序打开一个隐藏的下拉菜单

当我单击搜索按钮时,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;
    }


}
4

3 回答 3

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;
}
于 2012-12-07T09:28:57.910 回答
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);
}
于 2013-10-03T07:22:51.857 回答
0

只需在菜单的 xml 中,将属性设置为 app:showAsAction="collapseActionView|ifRoom" 用于 seachview,其他菜单项将其设置为 app:showAsAction="ifRoom"。这将为您提供预期的结果。

于 2014-11-07T09:07:58.373 回答