How to completely collapse a SearchView after an i

2019-01-31 19:58发布

I've been struggling with this for weeks.. I have a global search that offers up a custom listview with suggestions as a user types. When a user selects an option, I want the searchview to return to its completely collapsed state.

enter image description here

Instead, it shrinks down but stays in a slightly expanded view.

enter image description here

I've thrown EVERYTHING I can find at this thing to close it, but cannot for the life of me get the right method. Here's the function:

    final SearchView.OnCloseListener closeListener = new SearchView.OnCloseListener() {

        @Override
        public boolean onClose() {
            return closeSearch();
        }
    };

    protected boolean closeSearch() {
    _searchView.clearFocus();
    _searchView.setQuery("", false);
    _searchView.setFocusable(false);
    _searchMenuItem.collapseActionView();
    isSearchFragmentVisible(false);
    return false;
}

Close search is then manually called when an item is selected from the custom "suggestion" listview.

10条回答
做自己的国王
2楼-- · 2019-01-31 20:41

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)?

查看更多
成全新的幸福
3楼-- · 2019-01-31 20:42

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

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-31 20:42
searchView.setIconified(true); // Call this twice
searchView.setIconified(true);

It works for me.

查看更多
仙女界的扛把子
5楼-- · 2019-01-31 20:43

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

MenuItemCompat.collapseActionView(menuItem);
查看更多
登录 后发表回答