actionbar search will not close after search

前端 未结 2 650
[愿得一人]
[愿得一人] 2020-12-31 04:55

I have an actionbar with a search icon. When the search icon is clicked, it expands to a search bar in which the user can type in a search.

The problem is when the

相关标签:
2条回答
  • 2020-12-31 05:06

    One simpler way, that works even if the SearchView widget is not on the actionbar is the following code:

    searchView.setQuery("", false);
    searchView.setIconified(true);
    

    And that makes the SearchView collapse after a search is made.

    0 讨论(0)
  • 2020-12-31 05:21

    After some searches, I find the solution. The onActionViewCollapsed works but you had an unexpected behaviour (icon jumps to left, up indicator still here...) - this (hard) solution was suggested in my first and previous answer, and I was persuaded by use of collapseActionView method. However, SearchView.collapseActionView() was not working because according to the Documentation:

    Collapse the action view associated with this menu item.

    It's related to the MenuItem and not to the SearchView widget. That's why you had this error when you used this method:

    The method collapseActionView() is undefined for the type SearchView

    Then, the solution is to create a Menu variable as follows:

     // At the top of your class
     private Menu mMenu;
    
     // onCreateOptionsMenu method
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
          this.mMenu = menu; // init the variable
          // other stuff..
          return true;
     }
    
     // call the collapseActionView method
     public boolean onQueryTextSubmit(String query) {
          searchView.setIconified(true);
          searchView.clearFocus();
          // call your request, do some stuff..
    
          // collapse the action view
          if (mMenu != null) { 
              (mMenu.findItem(R.id.menu_search2)).collapseActionView();
          }
          return false;
     }
    

    Or another might be to avoid the implement SearchView.OnQueryTextListener and to do it inside onCreateOptionsMenu as follows:

    @Override
    // make your Menu as 'final' variable
    public void onCreateOptionsMenu (final Menu menu, MenuInflater inflater) {
        searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView();
        // call the query listener directly on the SearchView
        searchView.setOnQueryTextListener(new OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                searchView.setIconified(true);
                searchView.clearFocus();
    
                // call the request here
    
                // call collapse action view on 'MenuItem'
                (menu.findItem(R.id.menu_search2)).collapseActionView();
    
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) { return false; }
        });
        searchView.setIconified(false);
    }  
    

    This will resolve the issue for sure. Happy coding!

    0 讨论(0)
提交回复
热议问题