invalidateOptionsMenu在片段中不起作用

时间:2014-08-09 06:23:04

标签: android android-fragments android-actionbar

我想根据编辑文本中的文本显示或隐藏操作栏中的项目

所以我做了以下

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





}

但是动作项目永远不会出现

3 个答案:

答案 0 :(得分:63)

要更新片段内的onCreateOptionsMenu,您需要在片段的setHasOptionsMenu(true);方法中调用onCreate。否则,当您致电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);


}

答案 1 :(得分:8)

试试这个:

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

答案 2 :(得分:3)

解决方案1 ​​

这是片段中的作品

 getActivity().invalidateOptionsMenu();

解决方案2

如果上述代码不起作用,请尝试

invalidateOptionsMenu(getActivity());

导入此库

import static androidx.core.app.ActivityCompat.invalidateOptionsMenu;