android.support.v4.widget.SearchViewCompat example?

[亡魂溺海] 提交于 2019-12-10 01:06:56

问题


I am trying to use SearchViewCompat with ActionBarSherlock in an API 8 app.

public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem item = menu.add("Search")
        .setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.ic_search)
        .setActionView(R.layout.collapsible_edittext);
    item.setShowAsAction(
        MenuItem.SHOW_AS_ACTION_ALWAYS | 
        MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    // To use SearchViewCompat, I need to add it to the Menu item as well:
    View searchView = SearchViewCompat.newSearchView(this);
    // ...
    SearchViewCompat.setOnQueryTextListener(...);
    // ...
    item.setActionView(searchView);

Please note that both the top and bottom code needs to call setActionView(). Does that mean it is not possible to do search?


回答1:


If you are using the ActionBarSherlock Library ver 4.2, you can replace the API 11 SearchView Widget with a ActionBarSherlock SearchView Widget to make it backward compatible:

search.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/description_search"
        android:orderInCategory="0"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
        android:showAsAction="ifRoom|collapseActionView" /> 
</menu>

Activity class

//IMPORTANT!!!
import com.actionbarsherlock.widget.SearchView;

...

@Override 
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getSupportMenuInflater().inflate(R.menu.search, menu);
    setupSearchMenuItem(menu);
    return true;
}

private void setupSearchMenuItem(Menu menu) {
    MenuItem searchItem = menu.findItem(R.id.menu_search);
    if (searchItem != null) {
        SearchView searchView = (SearchView) searchItem.getActionView();
        if (searchView != null) {
            SearchManager searchManager = 
                 (SearchManager) getSystemService(SEARCH_SERVICE);
            searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
            }
        }
    }
}



回答2:


What is the actual problem? SearchViewCompat will return null for pre-HC devices since the SearchView widget does not exist. This means you will have to provide your own custom action view that imitates the HC SearchView.

You can also backport the SearchView component from the Android sources and use that.

Otherwise, you can just use the existing search interfaces Android has, in which case for HC+ devices you use the action view to perform a search but on Froyo and Gingerbread devices the user clicks on the search icon and a search bar animates from the top.

Hope this helps.




回答3:


At some point in your Activity:

public class HomeActivity extends SherlockFragmentActivity implements 
    SearchView.OnQueryTextListener {

// ...    
SearchView searchView = (com.actionbarsherlock.widget.SearchView) 
    actionBarCustom.findViewById(R.id.search);
SearchManager sm = (SearchManager)getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(sm.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);

And then filter your list adapter:

@Override
public boolean onQueryTextSubmit(String query) {
    return true;
}

@Override
public boolean onQueryTextChange(String newText) {
    mAdapter.getFilter().filter(newText.trim());
    return false;
}

This way, your list adapter must implement filterable.




回答4:


Better to use MenuItemCompat,I think this is helpful for you

    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchItem);
    SearchableInfo info =  searchManager.getSearchableInfo(getComponentName());
    searchView.setSearchableInfo(info);


来源:https://stackoverflow.com/questions/10222311/android-support-v4-widget-searchviewcompat-example

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