如何检测 SearchView 是否展开?

How to detect if SearchView is expanded?

我有如下菜单项:

<item
    android:id="@+id/action_search"
    android:title="search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always"/>

@Override
public boolean onCreateOptionsMenu( Menu menu ) {
  getMenuInflater().inflate( R.menu.passenger, menu );
  searchItem = menu.findItem( R.id.action_search );
  searchView = (SearchView)MenuItemCompat.getActionView( searchItem );
  searchView.setSubmitButtonEnabled( false );
  return super.onCreateOptionsMenu( menu );
}

我想检查 searchView 是否展开。

我尝试了 searchItem.isActionViewExpanded()searchView.isHovered(),但其中 none 成功了。

我错过了什么?

TIA

尝试searchItem.isIconified()

Returns the current iconified state of the SearchView.

Returns

true if the SearchView is currently iconified, false if the search field is fully visible.

http://developer.android.com/reference/android/widget/SearchView.html#isIconified%28%29

searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do what you want when search view expended
            }
        });
        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                //do what you want  searchview is not expanded
                return false;
            }
        });

试试这个,它会检测用户何时点击搜索图标:

在科特林中:

    searchView.setOnQueryTextFocusChangeListener { _ , hasFocus ->
        if (hasFocus) {
            // searchView expanded
        } else {
            // searchView not expanded
        }
    }

在Java中:

searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            // searchView expanded
        } else {
            // searchView not expanded
        }
    }
});