Android ActionbarSherlock SearchView

2019-02-02 00:44发布

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)

4条回答
Fickle 薄情
2楼-- · 2019-02-02 00:56

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

查看更多
该账号已被封号
3楼-- · 2019-02-02 01:00

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.

查看更多
叼着烟拽天下
4楼-- · 2019-02-02 01:06

I had a similar problem, so I created a custom SearchView. Maybe you want to check it out at https://github.com/benjaminmock/MenuItemSearchAction

查看更多
再贱就再见
5楼-- · 2019-02-02 01:13
<?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>
查看更多
登录 后发表回答