Using native look and feel of the searchView for actionBarSherlock on old Android versions

时光总嘲笑我的痴心妄想 提交于 2020-02-21 03:13:20

问题


I've noticed that there is no working solution that shows how to use filter on a listView items using the actionbar, that works on older Android versions (like 2.3.x).

The only example I've found is in the file "LoaderCursorSupport.java" of the fragments example. However, it only works when the searchView can be created, meaning starting from Android 3.x, as shown in the code:

View searchView=SearchViewCompat.newSearchView(getActivity());
if(searchView!=null)
...    

The above bug (or missing feature, whichever way you look at it) still exist even on version 4.2 of actionBarSherlock.

So I've made my own solution, which works great (and I wish the official library could add my fix to it too), but I don't know where to get the "x" button within the editText view that is responsible for clearing the text.

Can anyone please tell me how to get the native look and feel and put it correctly in the code?

Here's a screenshot of what i'm talking about:

For those wish to use this feature, here is my code snippet :

@Override
public boolean onCreateOptionsMenu(final com.actionbarsherlock.view.Menu menu)
  {
  getSupportMenuInflater().inflate(R.menu.activity_main,menu);
  _searchMenuItem=menu.findItem(R.id.menu_item_action_search);
  View searchView=SearchViewCompat.newSearchView(this);
  if(searchView!=null)
    SearchViewCompat.setOnQueryTextListener(searchView,new OnQueryTextListenerCompat()
      {
        @Override
        public boolean onQueryTextChange(final String newText)
          {
          _listAdapter.getFilter().filter(newText);
          return true;
          }

        @Override
        public boolean onQueryTextSubmit(final String query)
          {
          return super.onQueryTextSubmit(query);
          }
      });
  else
    {
    searchView=new EditText(this);
    searchView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    ((EditText)searchView).setHint(R.string.search);
    ((EditText)searchView).addTextChangedListener(new TextWatcher()
      {
        String curretTextToFilter =null;

        @Override
        public void onTextChanged(final CharSequence newText,final int start,final int before,final int count)
          {
          if(newText==curretTextToFilter)
            return;
          curretTextToFilter=newText.toString();
          _listAdapter.getFilter().filter(curretTextToFilter==null||curretTextToFilter.length()==0 ? null : curretTextToFilter);
          }

        @Override
        public void beforeTextChanged(final CharSequence s,final int start,final int count,final int after)
          {}

        @Override
        public void afterTextChanged(final Editable s)
          {}
      });
    }
  final View finalSearchView=searchView;
  _searchMenuItem.setOnActionExpandListener(new OnActionExpandListener()
    {
      @Override
      public boolean onMenuItemActionExpand(final MenuItem item)
        {
        if(finalSearchView instanceof EditText)
          {
          final InputMethodManager m=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
          finalSearchView.requestFocus();
          if(m!=null)
            m.toggleSoftInput(0,InputMethodManager.SHOW_IMPLICIT);
          }
        return true;
        }

      @Override
      public boolean onMenuItemActionCollapse(final MenuItem item)
        {
        if(finalSearchView instanceof EditText)
          ((EditText)finalSearchView).setText(null);
        else _listAdapter.getFilter().filter(null);
        return true;
        }
    });
  _searchMenuItem.setActionView(searchView);
  //
  return true;
  }

@Override
public boolean onKeyUp(final int keyCode,final KeyEvent event)
  {
  if(keyCode==KeyEvent.KEYCODE_SEARCH)
    {
    _searchMenuItem.expandActionView();
    return true;
    }
  return super.onKeyUp(keyCode,event);
  }

回答1:


Except for the "X" button, I think the rest can be done using a library with a theme, called HoloEverywhere.



来源:https://stackoverflow.com/questions/12457537/using-native-look-and-feel-of-the-searchview-for-actionbarsherlock-on-old-androi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!