Java Honeycomb 3.2 上的 android searchview setOnActionExpandListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19918500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android searchview setOnActionExpandListener on Honeycomb 3.2
提问by critical
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.
我正在开发适用于 Android 3.2 及更高版本的应用程序android-support-v4
。OnActionExpandListener
当操作栏中的 SearchView 展开和折叠时,我需要实现“拦截”。我的 Android 4.0 及更高版本的代码没问题,但对于 3.2 则不行。
menu.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" />
MyActivity.java
我的活动.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;
}
});
}
}
Why, for Honeycomb, methods of listener is not invoked?
为什么对于 Honeycomb,不调用 listener 的方法?
Thank you so much.
非常感谢。
采纳答案by Dan Danz
You probably missed the fact (like I did) that `MenuItemCompat.OnActionExpandListener' interface has a static implementation, and is not an instance method.
您可能错过了这样一个事实(就像我所做的那样)`MenuItemCompat.OnActionExpandListener' 接口有一个静态实现,而不是一个实例方法。
So, if you have a class that implements MenuItemCompat.OnActionExpandListener
then in that class you need to install it as the listener like this:
因此,如果您有一个实现类的类,MenuItemCompat.OnActionExpandListener
则需要在该类中将其安装为侦听器,如下所示:
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).
相同的范例适用于 setActionView ... 而不是 invoke menuItem.setActionView(this)
,您将 menuItem 作为第一个参数传递给静态版本,MenuItemCompat.setActionView
然后跟随其他参数。
回答by yajnesh
Your Listener should be MenuItemCompat.OnActionExpandListener() .
你的监听器应该是 MenuItemCompat.OnActionExpandListener() 。
MenuItemCompat.setOnActionExpandListener(searchItem,
new MenuItemCompat.OnActionExpandListener() {
}
回答by Fenil
For MenuItemCompat.setOnActionExpandListener to work you should add "collapseActionView" added in the menu item - for example -
要使 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" />
And in the onCreateOptionsMenu you can use it this way -
在 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
}
});
回答by Sir NIkolay Cesar The First
I found that MenuItemCompat.setOnActionExpandListener(...)
is not working if you don't pass:
我发现MenuItemCompat.setOnActionExpandListener(...)
如果你不通过,那是行不通的:
searchItem
.setShowAsAction(MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
But this is changing the SearchView and is replacing the DrawerToggle with back arrow.
但这正在改变 SearchView 并用后退箭头替换 DrawerToggle。
I wanted to keep the original views and still track the Expanded/Collapsed state and use supported Search View.
我想保留原始视图并仍然跟踪展开/折叠状态并使用支持的搜索视图。
Solution:
解决方案:
When android.support.v7.widget.SearchView
is changing the view state the LinearLayout view's, with id android.support.v7.appcompat.R.id.search_edit_frame
, visibility value is being changed from View.VISIBLE
to View.GONE
and opposite. So I add ViewTreeObserver to track the visibility change of the search edit frame.
当android.support.v7.widget.SearchView
更改 LinearLayout 视图的视图状态时,带有 id 的android.support.v7.appcompat.R.id.search_edit_frame
可见性值正在从View.VISIBLE
to更改为View.GONE
相反。所以我添加了 ViewTreeObserver 来跟踪搜索编辑框的可见性变化。
menu_search.xml:
menu_search.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
</menu>
In the activity:
在活动中:
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
..........
private View mSearchEditFrame;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = (MenuItem) menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(searchItem);
searchView.setSubmitButtonEnabled(false);
mSearchEditFrame = searchView
.findViewById(android.support.v7.appcompat.R.id.search_edit_frame);
ViewTreeObserver vto = mSearchEditFrame.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
int oldVisibility = -1;
@Override
public void onGlobalLayout() {
int currentVisibility = mSearchEditFrame.getVisibility();
if (currentVisibility != oldVisibility) {
if (currentVisibility == View.VISIBLE) {
Log.v(TAG, "EXPANDED");
} else {
Log.v(TAG, "COLLAPSED");
}
oldVisibility = currentVisibility;
}
}
});
return super.onCreateOptionsMenu(menu);
}
Thanks!
谢谢!
回答by Lei Leon
thanks for your help, your solution is work for me. and i'd like to vote you up, but i just realized i have only 1 reputation,(;′⌒`)
感谢您的帮助,您的解决方案对我有用。我想给你投票,但我才发现我只有 1 个声望,(;′⌒`)
actually, my solution is similar to your, there is just one different in the menu xml file like this:
实际上,我的解决方案与您的类似,菜单 xml 文件中只有一个不同之处,如下所示:
<item
android:id="@+id/apps_menu_search"
android:icon="@drawable/ic_action_search"
android:title="@string/apps_menu_search"
android:visible="true"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />