invalidateOptionsMenu不片段工作 [英] invalidateOptionsMenu doesn't work in fragment

查看:313
本文介绍了invalidateOptionsMenu不片段工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示或根据动作条隐藏的项目要么他们是在编辑文字的文本或不

I want to show or hide item in actionbar according to either their is text in the edit text or not

所以我做了以下

            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);
            }
    }





}

但actionitem不会出现

but the actionitem never appear

推荐答案

有关更新 onCreateOptionsMenu 你需要调用该片段中的 setHasOptionsMenu(真); invalidateOptionsMenu()里面没有它的片段的onCreate方法您将无法当你调用 getActivity()来更新它。

For updating the onCreateOptionsMenu inside the fragment you need to call the setHasOptionsMenu(true); inside the onCreate method of the fragment without it you wont be able to update it when you call getActivity().invalidateOptionsMenu();

示例:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

编辑:

@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);


}

这篇关于invalidateOptionsMenu不片段工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆