android searchview setOnActionExpandListener on Honeycomb 3.2

我正在为Android 3.2及更高版本开发一个应用程序。我需要在操作栏中的 SearchView 展开和折叠时实现“拦截”。我的Android 4.0及更高版本的代码还可以,但对于3.2没有。android-support-v4OnActionExpandListener

菜单.xml

<item android:id="@+id/menu_search"
    android:title="@string/menu_search"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="collapseActionView|always"
    android:actionViewClass="android.widget.SearchView" />

我的活动.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.reader, menu);

    final MenuItem searchMI = menu.findItem(R.id.menu_search);
    if(searchView == null) {
        //searchView = (SearchView) searchMI.getActionView();
        searchView = (SearchView) MenuItemCompat.getActionView(searchMI);
        searchView.setOnQueryTextListener(this);
        searchView.setOnSuggestionListener(this);
        searchView.setOnCloseListener(new OnCloseListener() {

            @Override
            public boolean onClose() {
                //some code
                return false;
            }
        });
    }


    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
        MenuItemCompat.setShowAsAction(searchMI, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
        MenuItemCompat.setOnActionExpandListener(searchMI, new OnActionExpandListener() {

            /* (non-Javadoc)
             * @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionExpand(android.view.MenuItem)
             */
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                Toast.makeText(getApplicationContext(), "onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
                return true;
            }

            /* (non-Javadoc)
             * @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionCollapse(android.view.MenuItem)
             */
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                Toast.makeText(getApplicationContext(), "onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    } else {
        searchMI.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                Toast.makeText(getApplicationContext(), "MenuItem#onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                Toast.makeText(getApplicationContext(), "MenuItem#onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}

为什么对于Honeycomb,不调用侦听器的方法?

非常感谢。


答案 1

你可能错过了一个事实(就像我一样),'MenuItemCompat.OnActionExpandListener'接口具有静态实现,而不是实例方法。

因此,如果你有一个实现的类,那么在该类中,你需要像这样将其安装为侦听器:MenuItemCompat.OnActionExpandListener

MenuItem menuItem =  menu.findItem(R.id.search);
if (menuItem != null) {
  MenuItemCompat.setOnActionExpandListener(menuItem,this);
  MenuItemCompat.setActionView(menuItem, mSearchView);
}

同样的范式也适用于 setActionView ...而不是调用 ,您将 menuItem 作为第一个参数传递给静态版本,然后跟随其他参数。menuItem.setActionView(this)MenuItemCompat.setActionView


答案 2

要使MenuItemCompat.setOnActionExpandListener正常工作,您应该添加“collapseActionView”添加到菜单项中 - 例如 -

<item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          app:showAsAction="ifRoom|collapseActionView"
          app:actionViewClass="android.support.v7.widget.SearchView" />

在onCreateOptionsMenu中,您可以以这种方式使用它 -

MenuItemCompat.setOnActionExpandListener(menu_search,
            new OnActionExpandListener()
            {
                @Override
                public boolean onMenuItemActionCollapse(MenuItem item)
                {
                    // Do something when collapsed
                    return true; // Return true to collapse action view
                }

                @Override
                public boolean onMenuItemActionExpand(MenuItem item)
                {
                    // Do something when expanded
                    return true; // Return true to expand action view
                }
            });

推荐