问题描述
简短:当我单击SearchViewIcon时,SearchView不会折叠/展开.
Short: When I click on my SearchViewIcon, the SearchView doesn't collapse/expand.
长:
我正在使用SearchView过滤MainActivity中的Fragment中的RecyclerView.
I"m using a SearchView to filter a RecyclerView in a Fragment that is in my MainActivity.
当我单击SearchViewIcon时(默认情况下SearchView被图标化).我使用以下代码打开带有正确片段的选项卡:
When I click on the SearchViewIcon (SearchView is iconified by default). I open the tab with the correct Fragment with this code:
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(2, false);
}
});
具有正确片段的选项卡将按预期方式打开.还显示了文本输入,但SearchView保持图标状态. (请参见下图.)
The Tab with the correct Fragment is opened like expected. Also the text input is shown, but the SearchView stays iconified. (See picture below.)
我的XML SearchView:
My SearchView in XML:
<item
android:id="@+id/action_search"
android:orderInCategory="1"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/menu_item_search_title"
app:showAsAction="ifRoom"
app:queryHint="Search name or function"
app:actionViewClass="android.support.v7.widget.SearchView" />
我已经尝试过的事情:
将我的showAsAction设置为always或ifRoom | collapseActionView
Setting my showAsAction to always or ifRoom|collapseActionView
app:showAsAction="always"
app:showAsAction="ifRoom|collapseActionView"
请求关注我的SearchView:
Request focus on my SearchView:
searchView.requestFocus();
展开我的SearchViewItem:
Expanding my SearchViewItem:
MenuItem searchViewItem = menu.findItem(R.id.action_search);
searchViewItem.expandActionView();
但是这些东西都不起作用...
But non of these things worked...
编辑如标题所示,如果再次单击SearchViewIcon,则SearchView会展开.
EDITLike the title says, if I click the SearchViewIcon again, the SearchView does expand.
推荐答案
我也遇到了类似的问题,并且能够解决它.我需要解决的主要问题是:
I've also encountered a similar problem, and was able to solve it.The main problems I've needed to solve were:
- searchView默认情况下未展开
- 直到第二次点击,搜索EditText才成为焦点
- 即使解决了(2)的问题,键盘也没有出现,直到第二次单击为止.
解决方案:
- menu.xml中的
- 需要将
app:showAsAction
定义为"collapseActionView"
+在Java代码中还需要调用searchView.setIconifiedByDefault(false)
进行扩展,以使其优先于图标化(即-按下图标时,展开并单击'不停留在图标模式) - 为您的
search menu-item object
添加一个MenuItem.OnActionExpandListener
,并使用处理程序发布可运行内容,该内容将请求焦点到您的search-view object
.(为什么可运行?因为不能保证菜单仍未完全膨胀时请求焦点无法正常工作.当在处理程序可运行时请求焦点时,我确保焦点请求在onCreateOptionsMenu()
中的所有工作准备就绪后发生完成. - 在(2)中定义的可运行的相同位置,也请android OS显示键盘.
- in menu.xml need to define
app:showAsAction
as"collapseActionView"
+ in java code need also to callsearchView.setIconifiedByDefault(false)
for the expansion to take priority over the iconification (that is - when the icon is pressed, expand and don't stay in icon mode) - add a
MenuItem.OnActionExpandListener
for yoursearch menu-item object
and use a handler to post a runnable that will request focus to yoursearch-view object
.(why runnable? because requesting focus when the menu is still not fully inflated is not guaranteed to work. When requesting focus in a handler-runnable, I'm making sure the focus request happens AFTER all the work in theonCreateOptionsMenu()
is ready and finished. - in The same defined runnable from (2), also ask android OS to show the keyboard.
解决所有这些问题的完整代码:
complete code solving all those problems:
<item
android:id="@+id/searchContacts"
android:icon="@drawable/ic_search_white_24dp"
android:title="search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.widget.SearchView"
/>
<!-- and of course your other menu items -->
需要创建这样的xml文件.右键单击您的res
文件夹,然后选择new --> android resource file
.将您想要的任何内容作为文件名(例如,"searchable"可以正常工作),然后选择XML
作为资源类型.然后复制&将此代码粘贴到创建的文件中(用您自己的提示字符串替换):
Need to create such xml file.right-click your res
folder and choose new --> android resource file
. put whatever you want as file name ("searchable" will work ok, for example), and choose XML
as resource type.Then copy & paste this code in the created file (replace the hint string with your own):
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="put here your hint string to be shown"
/>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
implementSearch(menu);
return true;
}
private void implementSearch(final Menu menu) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final MenuItem searchMenuItem = menu.findItem(R.id.searchContacts);
final SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener(){
@Override
public boolean onMenuItemActionExpand(MenuItem item){
// the search view is now open. add your logic if you want
new Handler().post(new Runnable() {
@Override
public void run() {
searchView.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) { // it's never null. I've added this line just to make the compiler happy
imm.showSoftInput(searchView.findFocus(), 0);
}
}
});
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item){
// the search view is closing. add your logic if you want
return true;
}
});
}
此外,如果您想使用自己的回调进行搜索的文本更改和提交(而不是Android的意图),请在implementSearch()
方法中,添加对searchMenuItem.setOnActionExpandListener(new SearchView.OnQueryTextListener({...}))
的调用.
Also, if you want to use your own callbacks for the search's text change and submitted (instead of Android intents), in the implementSearch()
method add a call to searchMenuItem.setOnActionExpandListener(new SearchView.OnQueryTextListener({...}))
.
view 此问题了解更多信息
view this SO question for more details
这篇关于Android SearchView只需单击两次即可扩展视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!