android - collapse searchview after submit

只谈情不闲聊 提交于 2019-12-05 01:29:47

You can do it this way in your activity, tested with actionbarsherlock (it even hides the keyboard, make sure to return false in onQueryTextSubmit):

private MenuItem searchMenuItem;

public MenuItem getSearchMenuItem() {
    return searchMenuItem;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // ...
    searchMenuItem = menu.findItem(R.id.menu_search);
    // ...
    searchView.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            MenuItem searchMenuItem = getSearchMenuItem();
            if (searchMenuItem != null) {
                searchMenuItem.collapseActionView();
            }
            return false;
        }
        @Override
        public boolean onQueryTextChange(String newText) {
            // ...
            return true;
        }
    });
    // ...
    return super.onCreateOptionsMenu(menu);
}

you need to call setIconified(true) twice to actually collapse your search view, with first call text is cleared with second call keyboard and search view get closed.

If you are using the SearchView in the OptionsMenu, you ca call invalidateOptionsMenu()

moh
//close suggestion list on query text submit
searchView.setIconified(true);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!