How to dismiss/close/collapse SearchView in ActionBar in MainActivity?

前端 未结 6 2018
无人共我
无人共我 2021-01-07 17:54

I\'m trying to clear and close SearchView after entering a value. I found a solution but it closes the search and won\'t perform any action if I try to search again.

6条回答
  •  旧巷少年郎
    2021-01-07 18:50

    Hi i faced a similar scenario and so i think this changed to code should do the trick.
    Hope this helps...:)

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_actions, menu);
    Menu mMenuItem = menu.findItem(R.id.action_search_loc);  // take a reference with menue item
    searchView = (SearchView) mMenuItem.getActionView(); //use that to find searchview
    searchView.setOnQueryTextListener(searchListener);
    
    return super.onCreateOptionsMenu(menu);
    }
    
    SearchView.OnQueryTextListener searchListener = new SearchView.OnQueryTextListener(){
    @Override
    public boolean onQueryTextChange(String arg0) {
        return false;
    }
    @Override
    public boolean onQueryTextSubmit(String query) {
        new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query +    "&lang=pl").execute();
        try {
            mMenuItem.collapseActionView(); //this will collapse your search view
        }
        catch(Exception ex){
            ex.printStackTrace();
            System.out.println(ex);
        }
        return true;
    }
    };
    

提交回复
热议问题