SearchView in ActionBar — problems with the *Up* button

前端 未结 3 765
南笙
南笙 2020-12-16 04:07

I am using the the SearchView in the ActionBar of the ListView. The magnifying glass can be touched, the SearchView shows

3条回答
  •  时光取名叫无心
    2020-12-16 05:01

    I struggle with this a little until I found the solution.

    Declare your menuItem like this, check the showAsAction attribute, type only ifRoom, if you set collapseActionView the widget will collapse and show the back button on the actionbar

    
     
    
    

    Set up your SearchView as usual, remember to Add setIconifiedByDefault this will make the icon to start up as an icon

    SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setIconifiedByDefault(true);
    searchView.setOnQueryTextListener(new SearchViewOnQueryListener());
    searchView.setOnCloseListener(new SearchViewOnCloseListener());
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    

    On your QueryListener is where you handle the end of your search like so, here is where you use onActionViewCollapse() which collapse back the ViewSearch

    @Override
    public boolean onQueryTextSubmit(String query) {
                makeSearchRequest(SEARCH_TYPE_KEYWORD, query);
            searchView.setQuery("", false);
            searchView.clearFocus();
            searchView.onActionViewCollapsed();
            buttonClearSearchResults.setVisibility(View.VISIBLE);
            return false;
        }
    

提交回复
热议问题