你可以试试这个,非常适合我。
在活动中像这样膨胀菜单。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
//REFRESH ADAPTER WITH NEW FILTERED LIST HERE
return false;
}
@Override
public boolean onQueryTextChange(String text) {
//REFRESH ADAPTER WITH NEW FILTERED LIST HERE
return false;
}
});
return true;
}
菜单应该看起来像这样才能工作:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:grupete="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
grupete:actionViewClass="android.support.v7.widget.SearchView"
android:orderInCategory="100"
android:title="@string/search"
grupete:showAsAction="ifRoom"/>
</menu>
希望我能帮助你!
此致,
佩德罗维奇90