简体   繁体   English

onCreateOptionsMenu() 在 Fragment 中不起作用

[英]onCreateOptionsMenu() does not work in Fragment

I am trying to implement a search Filter in my Fragment, but it does not let me.我正在尝试在我的 Fragment 中实现一个搜索过滤器,但它不允许我这样做。 I used the same code in Activity before and it worked.我之前在 Activity 中使用了相同的代码并且它有效。 Whenever I type something, the onCreateOptionsMenu() does not get called.每当我键入内容时,都不会调用onCreateOptionsMenu() The App looks like this : [![app Picture][1]][1] As you can see I type something in the top and it won't get filtered.该应用程序如下所示: [![app Picture][1]][1] 正如你所看到的,我在顶部输入了一些内容,它不会被过滤。

My Fragment code: `我的片段代码:`

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View RootView = inflater.inflate(R.layout.fragment_tab1, container, false);
    if(getArguments() != null){
        String yourText = getArguments().getString("interessen");
        System.out.println(yourText);
    }`

. . . . . .

 @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    inflater.inflate(R.menu.menuevents, menu);
    super.onCreateOptionsMenu(menu, inflater);
    MenuItem searchitem = menu.findItem(R.id.searchEvent);
    //searchitem.setVisible(false);
    final SearchView searchView = (SearchView) searchitem.getActionView();
    final List<Event> allEvents = new ArrayList<>();
    allEvents.addAll(eventList);

    searchitem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            eventList.clear();
            eventList.addAll(allEvents);
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {

            eventList.clear();
            eventList.addAll(allEvents);
            return true;
        }
    });
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            eventListAdapter.getFilter().filter(newText);
            return false;
        }

    });

I did not copy the EventListAdapter in, becuase this should work just fine.我没有复制EventListAdapter ,因为这应该可以正常工作。 Its just that the Fragment does not get Access to the SearchItem.只是 Fragment 无法访问 SearchItem。

Anybody?有人吗? :) [1]: https://i.stack.imgur.com/JkLcL.png :) [1]: https://i.stack.imgur.com/JkLcL.png

You've called setMenuVisibility() , which means your Fragment is specifically opting out of having its menu being shown.您已调用setMenuVisibility() ,这意味着您的 Fragment 专门选择不显示其菜单。 It is expected that when your menu isn't shown, onCreateOptionsMenu() is not triggered.预计当您的菜单未显示时,不会触发onCreateOptionsMenu()

Remove that line of code to have your menu actually be visible.删除该行代码以使您的菜单真正可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM