SearchView.OnCloseListener does not get invoked

后端 未结 9 634
慢半拍i
慢半拍i 2020-12-03 22:23

Im using an action bar and adding a searchView to it. I have implemented the searchView.onCLoseListener but this does not seem to be getting invoked. Any suggestions ?

9条回答
  •  孤城傲影
    2020-12-03 23:04

    Create the menu item with the app:showAsAction set to always.

    
    

    When creating the SearchView in the onCreateOptionsMenu method do something like this

    inflater.inflate(R.menu.menu_search, menu);
    final MenuItem item = menu.findItem(R.id.action_search);
    final SearchView search = (SearchView) item.getActionView();
    search.setQueryHint(getString(R.string.search_brand_item));
    search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
      @Override
      public boolean onQueryTextSubmit(String query) {
        // add your code
        return false;
      }
    
      @Override
      public boolean onQueryTextChange(String newText) {
        // add your code 
        return false;
      }
    });
    search.setOnCloseListener(new SearchView.OnCloseListener() {
      @Override
      public boolean onClose() {
        // add your code here
        return false;
      }
    });
    search.setIconifiedByDefault(true); // make sure to set this to true
    

    The search.setIconifiedByDefault(true) needs to be set to true to call the onClose() method on the SearchView.OnCloseListener() created above.

提交回复
热议问题