Android ActionbarSherlock SearchView
我们可以在Android 2.2中使用ActionbarSherlock来实现SeachView吗?
我正在使用以下代码,它在 3.0 中工作正常,但在 2.2 中不起作用
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.main_menu, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
// set up a listener for the refresh item
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// Do something
System.out.println("onQueryTextChange----------");
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// Do something
System.out.println("onQueryTextSubmit----------");
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return true;
}
我在 2.2 上收到以下错误
at 01-23 17:31:53.230: W/MenuInflater(20214): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.internal.view.menu.MenuInflaterImpl$MenuState.newInstance(MenuInflaterImpl.java:533)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.internal.view.menu.MenuInflaterImpl$MenuState.setItem(MenuInflaterImpl.java:497)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.internal.view.menu.MenuInflaterImpl$MenuState.addItem(MenuInflaterImpl.java:515)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.internal.view.menu.MenuInflaterImpl.parseMenu(MenuInflaterImpl.java:238)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.internal.view.menu.MenuInflaterImpl.inflate(MenuInflaterImpl.java:164)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.sample.styledactionbar.MainActivity.onCreateOptionsMenu(MainActivity.java:99)
01-23 17:31:53.230: W/MenuInflater(20214): at android.support.v4.app.FragmentActivity.dispatchCreateOptionsMenu(FragmentActivity.java:601)
01-23 17:31:53.230: W/MenuInflater(20214): at android.support.v4.app.FragmentActivity.invalidateOptionsMenu(FragmentActivity.java:706)
Can we implement SeachView in Android 2.2 using ActionbarSherlock.
I am using following code, it is working fine in 3.0 but not working in 2.2
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.main_menu, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
// set up a listener for the refresh item
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// Do something
System.out.println("onQueryTextChange----------");
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// Do something
System.out.println("onQueryTextSubmit----------");
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return true;
}
I am getting following error on 2.2
at 01-23 17:31:53.230: W/MenuInflater(20214): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.internal.view.menu.MenuInflaterImpl$MenuState.newInstance(MenuInflaterImpl.java:533)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.internal.view.menu.MenuInflaterImpl$MenuState.setItem(MenuInflaterImpl.java:497)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.internal.view.menu.MenuInflaterImpl$MenuState.addItem(MenuInflaterImpl.java:515)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.internal.view.menu.MenuInflaterImpl.parseMenu(MenuInflaterImpl.java:238)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.internal.view.menu.MenuInflaterImpl.inflate(MenuInflaterImpl.java:164)
01-23 17:31:53.230: W/MenuInflater(20214): at com.actionbarsherlock.sample.styledactionbar.MainActivity.onCreateOptionsMenu(MainActivity.java:99)
01-23 17:31:53.230: W/MenuInflater(20214): at android.support.v4.app.FragmentActivity.dispatchCreateOptionsMenu(FragmentActivity.java:601)
01-23 17:31:53.230: W/MenuInflater(20214): at android.support.v4.app.FragmentActivity.invalidateOptionsMenu(FragmentActivity.java:706)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个小更新:
Jake Wharton 目前正在
dev
分支中开发 ActionBarSherlock 4.2.0,并已将SearchView
向后移植到那里。它的功能有限(最值得注意的是 - 不支持SearchableInfo
)。但他正在致力于扩展功能。我不知道 Jake Wharton 向后移植
SearchView
的意图,并做了我的向后移植版本(可以在 github 上找到:这两个实现的方式基本相同。他们使用的大部分代码与 AOSP 相同。但也存在一些显着差异。
SearchView
实现(即,它在所有平台上都有优点和缺点)。SearchView
的原始本机版本,并且所有功能均按其描述的方式受支持在文档中。就我个人而言,我建议坚持使用 ActionBarSherlock 版本(这样更容易)。仅当您在具有 API 11 及更高版本的系统上需要这种功能齐全的行为时,才使用我的库。
A small update:
Jake Wharton is currently working on ActionBarSherlock 4.2.0 in
dev
branch and hasSearchView
backported there. It has limited functionality (most notable - no support forSearchableInfo
). But he is working on expanding functionality.I didn't know about Jake Wharton's intentions on backporting
SearchView
and did my version of backport (can be found on github: abs-search-view). My version also does not supportSearchableInfo
.These two implementations were done in mostly the same way. They use much of the same code taken from AOSP. But there are some notable differences.
SearchView
implementation on systems with API 11 and up (i.e. it's the same code with pros and cons on all platforms).SearchView
and all features are supported the way they are described in documentation.Personally, I'd recommend sticking with ActionBarSherlock version (it's just easier that way). And use my library only if you need this fully functional behavior on systems with API 11 and up.
不幸的是,根据我的阅读和尝试,ActionBarSherlock 不支持 SearchView 小部件。
Android 将在未来发布一个向后兼容包来支持这一点,但谁知道那会是什么时候。
请参阅该人本人的链接:
https://github.com/JakeWharton/ActionBarSherlock/issues/70
Unfortunately, from what I have read and tried, ActionBarSherlock does not support the SearchView widget.
Android is set to release a backwards compatibility package in the future supporting this, but who knows when that will be.
See this link from the man himself:
https://github.com/JakeWharton/ActionBarSherlock/issues/70
我遇到了类似的问题,所以我创建了一个自定义 SearchView。也许您想查看 https://github.com/benjaminmock/MenuItemSearchAction
I had a similar problem, so I created a custom SearchView. Maybe you want to check it out at https://github.com/benjaminmock/MenuItemSearchAction