[英]Android DialogFragment - implement SearchView widget from toolbar inflated menu
As the title of the question says, I am trying to implement the SearchView
widget placed in a toolbar of a DialogFragment
. 就像问题的标题所说的那样,我试图实现放置在DialogFragment
工具栏中的SearchView
小部件。 The menu is inflated using the inflateMenu
method of the toolbar. 使用工具栏的inflateMenu
方法来inflateMenu
菜单。
Note : The dialog fragment itself is called inside another Fragment, not an Activity 注意 :对话框片段本身是在另一个片段(而不是活动)中调用的
Here is how I call the dialogFragment: 这就是我所说的dialogFragment:
SearchFragment searchDialog = new SearchFragment();
searchDialog.setStyle(DialogFragment.STYLE_NORMAL, R.style.FullscreenDialogFragment);
searchDialog.show(getChildFragmentManager(), "search");
Here is my menu.xml
这是我的menu.xml
<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="com.hubsante.Fragment.SearchFragment">
<item
android:id="@+id/action_search"
android:title="Rechercher"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom" />
</menu>
And the implementation of the menu 并执行菜单
toolbar.inflateMenu(R.menu.menu_objective_details);
toolbar.setOnMenuItemClickListener(this);
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.action_search){
SearchManager manager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) item.getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getActivity().getComponentName()));
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getContext(), query, Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onQueryTextChange(String query) {
return true;
}
});
}
return true;
}
No toast is displayed when a query is submitted! 提交查询时不显示吐司! Even when I try to update a TextView inside the dialogfragment it is not updated. 即使我尝试在dialogfragment内部更新TextView,也不会更新。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu, menu);
final SearchView searchView= (SearchView) menu.findItem(R.id.action_search).getActionView(); searchView.setQueryHint(getResources().getString(R.string.search_view_helper));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
// Your toast message
return true;
}
@Override
public boolean onQueryTextChange(String input) {
return false;
}
});
return true;
}
Instead of trying to use a method onMenuItemClick, you can introduce the searchview in onCreateOptionsMenu() and set the listener in there. 可以尝试在onCreateOptionsMenu()中引入searchview并在其中设置侦听器,而不是尝试使用onMenuItemClick方法。 This is done in host activity class but you can override it in a fragment 这是在主机活动类中完成的,但是您可以在一个片段中覆盖它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.