我想知道如何仅在父片段中的操作栏中显示searchview。现在,当我从动作栏中的searchview提交搜索并转到下一个片段时,我仍然可以在我的子片段中看到searchview。有什么建议可以解决吗?感谢。
现在,在我的父类
中@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the options menu from XML
inflater.inflate(R.menu.homesearchbar, menu);
super.onCreateOptionsMenu(menu,inflater);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Bundle bundle = new Bundle();
bundle.putString("HomeToSearch_keyword", query);
SearchVC searchVC = new SearchVC();
searchVC.setArguments(bundle);
getFragmentManager().beginTransaction().add(R.id.HomeVC_frameChildFragment, searchVC)
.addToBackStack(null)
.commit();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
homesearchbar.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">
<item
android:id="@+id/action_search"
android:title=""
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
</menu>
答案 0 :(得分:1)
隐藏searchView
内的子片段override
内的onPrepareOptionsMenu
方法。然后在该方法中,您可以执行以下操作:menu.findItem(R.id.search).setVisible(false);
或最终在需要时将其更改为true
。另外,请不要忘记override onCreate
方法放置setHasOptionsMenu(true);