OnActionExpandListener

OnActionExpandListener

本文介绍了在蜂巢3.2的Andr​​oid搜索查看setOnActionExpandListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发与为Android 3.2和更高的应用程序的Andr​​oid支持-V4 。我需要实现 OnActionExpandListener 为拦截时,搜索查看,在动作条膨胀,当处于折叠状态。我的code为Android 4.0及更高版本它的确定,但对于3.2没有。

 <项目机器人:ID =@ + ID / menu_search
    机器人:标题=@字符串/ menu_search
    机器人:图标=@机器人:可绘制/ ic_menu_search
    机器人:showAsAction =collapseActionView |总是
    机器人:actionViewClass =android.widget.SearchView/>
 
  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    。getMenuInflater()膨胀(R.menu.reader,菜单);

    最终的菜单项searchMI = menu.findItem(R.id.menu_search);
    如果(搜索查看== NULL){
        //搜索查看=(搜索查看)searchMI.getActionView();
        搜索查看=(搜索查看)MenuItemCompat.getActionView(searchMI);
        sea​​rchView.setOnQueryTextListener(本);
        sea​​rchView.setOnSuggestionListener(本);
        sea​​rchView.setOnCloseListener(新OnCloseListener(){

            @覆盖
            公共布尔的OnClose(){
                //一些code
                返回false;
            }
        });
    }


    INT currentapiVersion = android.os.Build.VERSION.SDK_INT;
    如果(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,新OnActionExpandListener(){

            / *(非Javadoc中)
             * @看到android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionExpand(android.view.MenuItem)
             * /
            @覆盖
            公共布尔onMenuItemActionExpand(菜单项项){
                Toast.makeText(getApplicationContext(),onMenuItemActionExpand,Toast.LENGTH_SHORT).show();
                返回true;
            }

            / *(非Javadoc中)
             * @看到android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionCollapse(android.view.MenuItem)
             * /
            @覆盖
            公共布尔onMenuItemActionCollapse(菜单项项){
                Toast.makeText(getApplicationContext(),onMenuItemActionExpand,Toast.LENGTH_SHORT).show();
                返回true;
            }
        });
    } 其他 {
        sea​​rchMI.setOnActionExpandListener(新MenuItem.OnActionExpandListener(){

            @覆盖
            公共布尔onMenuItemActionExpand(菜单项项){
                Toast.makeText(getApplicationContext(),菜单项#onMenuItemActionExpand,Toast.LENGTH_SHORT).show();
                返回true;
            }

            @覆盖
            公共布尔onMenuItemActionCollapse(菜单项项){
                Toast.makeText(getApplicationContext(),菜单项#onMenuItemActionExpand,Toast.LENGTH_SHORT).show();
                返回true;
            }
        });
    }
}
 

为什么,蜂窝,监听器的方法不会被调用?

感谢你了。

解决方案

您可能错过的事实(像我一样),那`MenuItemCompat.OnActionExpandListener'接口有一个静态的实现,而不是一个实例方法。

所以,如果你有一个类实现 MenuItemCompat.OnActionExpandListener 然后在类,你需要安装它作为像这样的监听器:

 菜单项菜单项= menu.findItem(R.id.search);
    如果(菜单项!= NULL){
        MenuItemCompat.setOnActionExpandListener(菜单项,这一点);
        MenuItemCompat.setActionView(菜单项,mSearchView);
    }
 

同样的模式适用于setActionView ...而不是调用 menuItem.setActionView(本),你通过菜单项作为第一个参数的静态版本 MenuItemCompat.setActionView ,并按照与其他参数(S)。

I'm developing an app for Android 3.2 and greater with android-support-v4. I need to implement OnActionExpandListener for "intercept" when SearchView in the actionbar is expanded and when is collapsed. My code for Android 4.0 and higher it's ok, but for 3.2 no.

<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" />
@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;
            }
        });
    }
}

Why, for Honeycomb, methods of listener is not invoked?

Thank you so much.

解决方案

You probably missed the fact (like I did) that `MenuItemCompat.OnActionExpandListener' interface has a static implementation, and is not an instance method.

So, if you have a class that implements MenuItemCompat.OnActionExpandListener then in that class you need to install it as the listener like this:

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

The same paradigm applies to setActionView ... rather than invoke menuItem.setActionView(this), you pass the menuItem as the first argument to the static version MenuItemCompat.setActionView and follow with the other argument(s).

这篇关于在蜂巢3.2的Andr​​oid搜索查看setOnActionExpandListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 05:10