【发布时间】:2016-10-13 23:50:59
【问题描述】:
我需要在我的 SearchView 是否崩溃时进行拦截,以便更改我的 Activity 中的一些片段,即 AppCompatActivity。 我使用 MenuItemCompat.setOnActionExpandListener 方法创建了一个侦听器,但似乎从未调用过它。 同样奇怪的是,在我的方法 onOptionsItemSelected 中,经过一些调试,我检查了仍然在这里我什么都不能接收。我尝试了这里建立的各种方法来解决这个问题,但没有办法解决它。 这是我的代码:
menu_search_cat.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".CategoriesActivity">
<item android:id="@+id/search"
android:title="@string/search_title"
android:orderInCategory="1"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.widget.SearchView" />
类别活动
import android.widget.SearchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
...
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search_cat, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(this);
MenuItem searchItem = menu.findItem(R.id.search);
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onSearchViewExpand");
}
toggleDown();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
SuggestionsFragment newFragment = SuggestionsFragment.newInstance(mBusiness);
ft.replace(R.id.category_container, newFragment, "suggestionsFragment");
ft.addToBackStack(null);
ft.commit();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onSearchViewCollapse");
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
MacroCategoriesFragment newFragment = new MacroCategoriesFragment();
ft.replace(R.id.category_container, newFragment, "macrocatFragment");
ft.commit();
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onOptionsItemSelected: "+item.toString());
}
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
if (item.getItemId() == R.id.search) {
//never work here
}
return super.onOptionsItemSelected(item);
}
【问题讨论】:
标签: android android-appcompat searchview