动作条搜索后不会关闭 [英] actionbar search will not close after search
问题描述
我有一个搜索图标的动作条。当搜索点击图标,它扩展到一个搜索栏,其中用户可以输入一个搜索。
当用户进入搜索,我想搜索栏可以折叠回一个图标,但我不能让它发生,我的生活的问题是。
我的动作条菜单看起来是这样的:
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
工具:上下文=MainActivity。>
<项目机器人:ID =@ + ID / menu_search2
机器人:actionViewClass =android.widget.SearchView
机器人:标题=搜索
机器人:图标=@机器人:可绘制/ ic_menu_search
机器人:showAsAction =总是| collapseActionView |
机器人:的onClick =goToSearch
/>
<项目机器人:ID =@ + ID / action_scan
机器人:图标=@可绘制/条code
机器人:的onClick =scanBar code
机器人:showAsAction =ifRoom | collapseActionView
/>
< /菜单>
我的搜索活动看起来是这样的:
公共类搜索扩展片段实现SearchView.OnQueryTextListener,ReadJSONResult.OnArticleSelectedListener {
私人的ListView LV;
视图V;
搜索查看搜索查看;
私人搜索查看mSearchView;
私人菜单项mSearchMenuItem;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
//设置布局在这里
V = inflater.inflate(R.layout.activity_search,集装箱,假);
setHasOptionsMenu(真正的);
getActivity()的setTitle(搜索)。
//获取用户信息
共享preferences preFS = preferenceManager.getDefaultShared preferences(getActivity());
字符串username = prefs.getString(username的,NULL);
字符串userid = prefs.getString(用户ID,NULL);
返回伏;
}
@覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
//充气菜单;这增加了项目操作栏,如果它是present。
搜索查看=(搜索查看)menu.findItem(R.id.menu_search2).getActionView();
searchView.setOnQueryTextListener(本);
searchView.setIconified(假);
}
公共布尔onQueryTextSubmit(查询字符串){
//敬酒查询
//使JSON的变量,以填补
searchView.setIconified(真正的);
searchView.clearFocus();
// URL进行申请
字符串URL =myURL;
尝试 {
查询= URLEn coder.en code(查询,UTF-8);
}赶上(UnsupportedEncodingException E){
// TODO自动生成的catch块
e.printStackTrace();
}
字符串jsonUrl =网址+查询;
// TODO:让JSON
ReadJSONResult任务=新ReadJSONResult(getActivity());
task.setOnArticleSelectedListener(本);
task.execute(jsonUrl);
返回false;
}
@覆盖
公共布尔onQueryTextChange(字符串newText){
// TODO自动生成方法存根
返回false;
}
@覆盖
公共无效onArticleSelected(字符串B,字符串啤酒厂){
searchView.setIconified(真正的);
searchView.clearFocus();
searchView.postInvalidate();
// code上单击执行
片段Fragment_one;
FragmentManager人= getFragmentManager();
FragmentTransaction TRAN = man.beginTransaction();
//增加啤酒的数据共享preFS啤酒标签
共享preferences preferences = preferenceManager.getDefaultShared preferences(getActivity());
共享preferences.Editor编辑器= preferences.edit();
editor.putString(beerID,B);
editor.putString(breweryID,啤酒厂);
editor.commit();
Fragment_one =新BeerTabs();
tran.replace(R.id.main,Fragment_one); // TRAN。
tran.addToBackStack(空);
tran.commit();
}
}
经过一番搜索,我找到了解决办法。该 onActionViewCollapsed
的作品,但你有一个意外的行为(图标跳到左,上指示灯还在这里...)的 - 这(硬)解决方案,建议在我的第一和previous答案的,而我的劝说下使用 collapseActionView
方法。然而, SearchView.collapseActionView()
是行不通的,因为根据的文档:
关闭是该菜单项关联的操作视图。
它关系到菜单项
,而不是在搜索查看
小部件。这就是为什么当你用这种方法,你有这个错误:
该方法的 collapseActionView()的是未定义的类型搜索查看
随后,该解决方案是创建一个菜单
变量如下:
//在你的类的顶部
私人菜单mMenu;
// onCreateOptionsMenu方法
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
this.mMenu =菜单; //初始化变量
// 其他的东西..
返回true;
}
//调用collapseActionView方法
公共布尔onQueryTextSubmit(查询字符串){
searchView.setIconified(真正的);
searchView.clearFocus();
//调用你的要求,做一些东西..
//折叠动作视图
如果(mMenu!= NULL){
(mMenu.findItem(R.id.menu_search2))collapseActionView()。
}
返回false;
}
或者另一种可能是,以避免实施 SearchView.OnQueryTextListener
并做内 onCreateOptionsMenu
如下:
@覆盖
//使你的菜单为最终变
公共无效onCreateOptionsMenu(最后菜单菜单,MenuInflater充气){
搜索查看=(搜索查看)menu.findItem(R.id.menu_search2).getActionView();
//直接调用查询监听器上的搜索查看
searchView.setOnQueryTextListener(新OnQueryTextListener(){
@覆盖
公共布尔onQueryTextSubmit(查询字符串){
searchView.setIconified(真正的);
searchView.clearFocus();
//这里调用请求
在菜单项//调用崩溃动作视图
(menu.findItem(R.id.menu_search2))collapseActionView()。
返回false;
}
@覆盖
公共布尔onQueryTextChange(字符串newText){返回false; }
});
searchView.setIconified(假);
}
这将解决这个问题是肯定的。编码快乐!
I have an actionbar with a search icon. When the search icon is clicked, it expands to a search bar in which the user can type in a search.
The problem is when the user enters the search I would like the search bar to collapse back to an icon but I can not get it to happen for the life of me.
My actionbar menu looks like this:
<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>
My search activity looks like this:
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();
}
}
After some searches, I find the solution. The onActionViewCollapsed
works but you had an unexpected behaviour (icon jumps to left, up indicator still here...) - this (hard) solution was suggested in my first and previous answer, and I was persuaded by use of collapseActionView
method. However, SearchView.collapseActionView()
was not working because according to the Documentation:
Collapse the action view associated with this menu item.
It's related to the MenuItem
and not to the SearchView
widget. That's why you had this error when you used this method:
The method collapseActionView() is undefined for the type SearchView
Then, the solution is to create a Menu
variable as follows:
// 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;
}
Or another might be to avoid the implement SearchView.OnQueryTextListener
and to do it inside onCreateOptionsMenu
as follows:
@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);
}
This will resolve the issue for sure. Happy coding!
这篇关于动作条搜索后不会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!