Android invalidateOptionsMenu 在片段中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25215853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
invalidateOptionsMenu doesn't work in fragment
提问by AMH
I want to show or hide item in actionbar according to either their is text in the edit text or not
我想根据编辑文本中的文本显示或隐藏操作栏中的项目
so I did the following
所以我做了以下
public class NounSearch extends android.app.Fragment
{
EditText seachEditText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.nounsearchactivity, container, false);
//Intiate EditText
seachEditText =(EditText) rootView.findViewById(R.id.nounSearch);
seachEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
searchResult.Entities = new ArrayList<NounModel>();
_currentPage = 0;
categoryId = -1;
new GetNouns().execute();
return true;
}
return false;
}
});
seachEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
getActivity().invalidateOptionsMenu();
}
});
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if(seachEditText.getText().toString().length() > 0)
{
menu.findItem(R.id.action_search).setVisible(true);
}
else
{
menu.findItem(R.id.action_search).setVisible(false);
}
}
}
but the actionitem never appear
但 actionitem 从未出现
回答by Rod_Algonquin
For updating the onCreateOptionsMenu
inside the fragment you need to call the setHasOptionsMenu(true);
inside the onCreate
method of the fragment. Otherwise you won't be able to update it when you call getActivity().invalidateOptionsMenu();
要更新onCreateOptionsMenu
片段内部,您需要调用片段的setHasOptionsMenu(true);
内部onCreate
方法。否则,您将无法在调用时更新它getActivity().invalidateOptionsMenu();
sample:
样本:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
EDIT:
编辑:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if(seachEditText.getText().toString().length() > 0)
{
menu.findItem(R.id.action_search).setVisible(true);
}
else
{
menu.findItem(R.id.action_search).setVisible(false);
}
super.onCreateOptionsMenu(menu, inflater);
}
回答by Akash Moradiya
Try this:
尝试这个:
@Override
public boolean onPrepareOptionsMenu(Menu menu, MenuInflater inflater) {
super.onPrepareOptionsMenu(menu, inflater);
if (seachEditText.getText().toString().length() > 0) {
menu.findItem(R.id.action_search).setVisible(true);
} else {
menu.findItem(R.id.action_search).setVisible(false);
}
}