How to completely collapse a SearchView after an item selected?

别说谁变了你拦得住时间么 提交于 2019-12-02 17:32:35

Instead of calling _searchView.onActionViewCollapsed() call menuItem.collapseActionView() where _searchView = menuItem.getActionView().

Found it. The call should be _searchView.onActionViewCollapsed(). Why on earth you call a "listener" style method (ie, one that begins with 'on') is beyond me.

@sebastian that's not actually right.

I've been stucked at this issue for a while, but finally I've managed to handle it in the right way. You're suppose to call menuSearch.collapseActionView(); instead. This method will call onActionViewCollapsed, which you could override. So you don't call a listener ;)

MenuItem menuSearch = menu.findItem(R.id.itemSearch);
SearchView searchView = (SearchView) menuSearch.getActionView();
//Don't use next line
//searchView.onActionViewCollapsed();
menuSearch.collapseActionView();

If you're using Toolbar (android.support.v7.widget.Toolbar) and the corresponding SearchView (android.support.v7.widget.SearchView) with it, this works:

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

You should clear the search, remove the focus and then call:

searchView.setIconified(true);

This worked for me and it also closed the keyboard by Default

MenuItemCompat.collapseActionView(menuItem);

Returning false should have been sufficient based on the latest documentation and the source code iff iconifiedByDefault == true.

Did you by any chance define iconifiedByDefault="false" or called setIconifiedByDefault(false)?

searchView.setIconified(true); // Call this twice
searchView.setIconified(true);

It works for me.

I've changed the 'Go' button to 'Next', so the only method that worked for me (on API 19) is typing closeBtn.performClick(); closeBtn.performClick(); (Clicking 'close' button twice) in the OnEditorActionListener of the AutoCompleteTextView. You can get the AutoCompleteTextView like this:

LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0);
LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2);
LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1);
AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);

This worked for me searchMenuItem.collapseActionView();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!