Android ActionbarSherlock SearchView

发布于 2024-12-28 09:05:02 字数 2456 浏览 1 评论 0原文

我们可以在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羁绊已千年 2025-01-04 09:05:02

一个小更新:

  • Jake Wharton 目前正在 dev 分支中开发 ActionBarSherlock 4.2.0,并已将 SearchView 向后移植到那里。它的功能有限(最值得注意的是 - 不支持 SearchableInfo)。但他正在致力于扩展功能。

  • 我不知道 Jake Wharton 向后移植 SearchView 的意图,并做了我的向后移植版本(可以在 github 上找到:

这两个实现的方式基本相同。他们使用的大部分代码与 AOSP 相同。但也存在一些显着差异。

  • ActionBarSherlock 向后移植支持建议适配器
  • ActionBarSherlock 向后移植不会回退到具有 API 11 及更高版本的系统上的原始 SearchView 实现(即,它在所有平台上都有优点和缺点)。
  • 我的版本不支持建议适配器(不过可以添加)。
  • 我的版本在 API 11 之前的系统(使用向后移植版本的代码)和 API 11 及更高版本上的功能有限,所有功能都会回退到 SearchView 的原始本机版本,并且所有功能均按其描述的方式受支持在文档中。

就我个人而言,我建议坚持使用 ActionBarSherlock 版本(这样更容易)。仅当您在具有 API 11 及更高版本的系统上需要这种功能齐全的行为时,才使用我的库。

A small update:

  • Jake Wharton is currently working on ActionBarSherlock 4.2.0 in dev branch and has SearchView backported there. It has limited functionality (most notable - no support for SearchableInfo). 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 support SearchableInfo.

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.

  • ActionBarSherlock backport supports suggestions adapter
  • ActionBarSherlock backport does not fallback on original SearchView implementation on systems with API 11 and up (i.e. it's the same code with pros and cons on all platforms).
  • My version does not support suggestions adapter (it can be added though).
  • My version has limited functionality on systems before API 11 (where backport version of code is used) and on API 11 and higher everything fallbacks to original native version of 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.

找回味觉 2025-01-04 09:05:02
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
    android:id="@+id/search"
    android:actionLayout="@layout/layout_menu_search"
    android:actionViewClass="com.actionbarsherlock.widget.SearchView"
    android:icon="@drawable/ic_menu_search"
    android:orderInCategory="1"
    android:showAsAction="always|collapseActionView"/>

</menu>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
    android:id="@+id/search"
    android:actionLayout="@layout/layout_menu_search"
    android:actionViewClass="com.actionbarsherlock.widget.SearchView"
    android:icon="@drawable/ic_menu_search"
    android:orderInCategory="1"
    android:showAsAction="always|collapseActionView"/>

</menu>
墟烟 2025-01-04 09:05:02

不幸的是,根据我的阅读和尝试,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

攀登最高峰 2025-01-04 09:05:02

我遇到了类似的问题,所以我创建了一个自定义 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文