【问题标题】:OnActionExpandListener in menuitem not working. How to fix it?menuitem 中的 OnActionExpandListener 不起作用。如何解决?
【发布时间】:2013-09-12 10:56:43
【问题描述】:

我正在开发一个从 API 级别 14 运行的 Android 应用程序。

我的菜单文件中有一个项目

  <item
        android:id="@+id/menu_search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/ic_action_action_search"
        android:orderInCategory="3"
         android:showAsAction="always"/>

我想知道这个搜索视图的展开和折叠,我试过这段代码,但它不起作用

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_search, menu);
            MenuItem mSearchItem = menu.findItem(R.id.menu_search);

            mSearchItem.setOnActionExpandListener( new OnActionExpandListener() {

                @Override
                public boolean onMenuItemActionExpand(MenuItem item) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this, "onMenuItemActionExpand", 1)
                     .show();
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem item) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this, "onMenuItemActionExpand", 1)
                     .show();
                    return true;

                }
            });
    }

}

为什么我没有得到祝酒词?

我也尝试了Handling collapsible action views的代码形式,它也不起作用!为什么这些东西不起作用?

编辑

我可以使用它来工作

mSearchView.addOnLayoutChangeListener(new OnLayoutChangeListener() {

            @Override
            public void onLayoutChange(View v, int left, int top, int right,
                    int bottom, int oldLeft, int oldTop, int oldRight,
                    int oldBottom) {

                SearchView searchView = (SearchView) v;
                if (searchView.isIconified()) {
                       }
                       else{
                        }
           }
   });

但是速度很慢!

【问题讨论】:

  • 2 小时后我发现我需要使用 android:showAsAction="always|collapseActionView"
  • 我刚刚发布了您的评论作为答案,以便其他人可以看到。

标签: java android android-actionbar searchview


【解决方案1】:

遇到类似问题的解决方案是(由提问者评论,我只是假设它可以得到更多强调):

android:showAsAction="always|collapseActionView" 添加到您的菜单项。

 <item android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        android:showAsAction="always|collapseActionView"
        android:actionViewClass="android.widget.SearchView"
        android:queryHint = "@string/search_hint"/>

【讨论】:

  • 这对我来说禁用了展开、折叠功能并且只显示静态搜索图标按钮!有什么建议吗?
  • @Learner_Programmer 这是解决方法:return true 而不是onMenuItemActionExpand 中的false
【解决方案2】:
Toast.makeText(MainActivity.this, "onMenuItemActionExpand", 1).show();

建议使用Toast.LENGTH_SHORTToast.LENGTH_LONG 而不是您的“1”。

示例:

Toast.makeText(MainActivity.this, "onMenuItemActionExpand", Toast.LENGTH_SHORT).show();

【讨论】:

  • 感谢您的建议,1 是 Toast.LENGTH_LONG 的常量值,请参阅 developer.android.com/reference/android/widget/… 任何方式,我只是将其用于测试..
  • 我不使用数字。日食只是不断显示警告。我还检查了您的主要问题,但是 github 上的两个项目完全像您一样使用它。您是否尝试过调试或一些 logcat 输出来限制邪恶的来源?
  • 我也使用了 logcat-outputs 但没有成功。能否提供github项目的链接?
  • 虽然不在 github 上,但这里是链接! code.google.com/p/frank-gairal-android/source/browse/trunk/src/…
  • 它正在使用 ActionBarSherlock 我不需要 ActionBarSherlock
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
相关资源
最近更新 更多