我有一个带搜索图标的操作栏。单击搜索图标后,它将扩展到用户可以键入搜索的搜索栏。
问题是,当用户输入搜索时,我希望搜索栏折叠回图标,但我无法让它在我的生活中发生。
我的操作栏菜单如下所示:
<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=".MainActivity" >
<item android:id="@+id/menu_search2"
android:actionViewClass="android.widget.SearchView"
android:title="Search"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="always|collapseActionView|"
android:onClick="goToSearch"
/>
<item android:id="@+id/action_scan"
android:icon="@drawable/barcode"
android:onClick="scanBarcode"
android:showAsAction="ifRoom|collapseActionView"
/>
</menu>
我的搜索活动如下:
public class Search extends Fragment implements SearchView.OnQueryTextListener, ReadJSONResult.OnArticleSelectedListener {
private ListView lv;
View v;
SearchView searchView;
private SearchView mSearchView;
private MenuItem mSearchMenuItem;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//set layout here
v = inflater.inflate(R.layout.activity_search, container, false);
setHasOptionsMenu(true);
getActivity().setTitle("Search");
//get user information
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String userName = prefs.getString("userName", null);
String userID = prefs.getString("userID", null);
return v;
}
@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView();
searchView.setOnQueryTextListener(this);
searchView.setIconified(false);
}
public boolean onQueryTextSubmit (String query) {
//toast query
//make json variables to fill
searchView.setIconified(true);
searchView.clearFocus();
// url to make request
String url = "myURL";
try {
query = URLEncoder.encode(query, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String jsonUrl = url + query;
//todo: get json
ReadJSONResult task = new ReadJSONResult(getActivity());
task.setOnArticleSelectedListener(this);
task.execute(jsonUrl);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onArticleSelected(String b, String brewery){
searchView.setIconified(true);
searchView.clearFocus();
searchView.postInvalidate();
//code to execute on click
Fragment Fragment_one;
FragmentManager man= getFragmentManager();
FragmentTransaction tran = man.beginTransaction();
//adds beer data to shared prefs for beer tabs
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("beerID",b);
editor.putString("breweryID",brewery);
editor.commit();
Fragment_one = new BeerTabs();
tran.replace(R.id.main, Fragment_one);//tran.
tran.addToBackStack(null);
tran.commit();
}
}
答案 0 :(得分:33)
一种更简单的方法,即使SearchView小部件不在操作栏上也可以使用以下代码:
searchView.setQuery("", false);
searchView.setIconified(true);
这使得SearchView在搜索完成后崩溃。
答案 1 :(得分:14)
经过一些搜索,我找到了解决方案。 onActionViewCollapsed
有效,但你有一个意想不到的行为(图标跳到左边,指示符仍然在这里......) - 这个(硬)解决方案在我的第一个和上一个回答中被提出,并且我使用collapseActionView
方法说服了我。但是,SearchView.collapseActionView()
无效,因为根据Documentation:
折叠与此菜单项关联的操作视图。
它与MenuItem
相关,而与SearchView
小部件无关。这就是使用此方法时出现此错误的原因:
类型SearchView
未定义方法 collapseActionView()
然后,解决方案是创建一个Menu
变量,如下所示:
// At the top of your class
private Menu mMenu;
// onCreateOptionsMenu method
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu; // init the variable
// other stuff..
return true;
}
// call the collapseActionView method
public boolean onQueryTextSubmit(String query) {
searchView.setIconified(true);
searchView.clearFocus();
// call your request, do some stuff..
// collapse the action view
if (mMenu != null) {
(mMenu.findItem(R.id.menu_search2)).collapseActionView();
}
return false;
}
或者另一个可能是避免实现SearchView.OnQueryTextListener
并在onCreateOptionsMenu
内执行,如下所示:
@Override
// make your Menu as 'final' variable
public void onCreateOptionsMenu (final Menu menu, MenuInflater inflater) {
searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView();
// call the query listener directly on the SearchView
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
searchView.setIconified(true);
searchView.clearFocus();
// call the request here
// call collapse action view on 'MenuItem'
(menu.findItem(R.id.menu_search2)).collapseActionView();
return false;
}
@Override
public boolean onQueryTextChange(String newText) { return false; }
});
searchView.setIconified(false);
}
这肯定会解决问题。快乐的编码!