简体   繁体   English

onOptionsItemSelected不注册片段中的项目

[英]onOptionsItemSelected not registering for items In Fragment

I have a fragment with a NavigationView which is using a menu with three items. 我有一个带有NavigationView的片段,该片段正在使用包含三个项目的菜单。 However, whenever I click the menu items, the onOptionsItemSelected() function is not called. 但是,每当我单击菜单项时,均不会调用onOptionsItemSelected()函数。

This NavigationView is being used in a drawer and the onOptionsItemSelected() function works when opening and closing the drawer, just not when clicking the menu items. NavigationView在抽屉中使用,并且onOptionsItemSelected()函数在打开和关闭抽屉时起作用,而不是在单击菜单项时起作用。

Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。

Fragment Class: 片段类别:

public class Navigation extends Fragment {

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

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

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_navigation, container, false);

    mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawerLayout);
    mToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, R.string.open, R.string.close);

    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();

    ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    return view;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //Opens and closes drawer
    if(mToggle.onOptionsItemSelected(item))
    {
        return true;
    }

    switch(item.getItemId())
    {
        case R.id.new_flight: Log.d("SELECTED", "new_flight");
            break;
        case R.id.saved_flight: Log.d("SELECTED", "saved_flight");
            break;
        case R.id.settings: Log.d("SELECTED", "settings");
            break;
        default:
            break;
    }

    return super.onOptionsItemSelected(item);
    }
}

Fragment XML: 片段XML:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.craig.flightfinder.Navigation"
    android:id="@+id/drawerLayout">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/mainNav"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:clickable="true"
        app:menu="@menu/navigation"></android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

Override OnCreateOptionsMenu inside your fragment. 覆盖片段中的OnCreateOptionsMenu From your code for fragment, it looks like you are not inflating the menu resource inside your fragment. 从片段的代码来看,您似乎没有在片段中增加菜单资源。

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.my_menu, menu);
}

where my_menu resource file has your items with the desired ids 其中my_menu资源文件中包含具有所需ID的项目

1.add your_menu.xml at res/menu folder. your_menu.xmlres/menu文件夹中添加your_menu.xml

2.Add onCreateOptionsMenu 2.添加onCreateOptionsMenu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.your_menu, menu);
}

override onNavigationItemSelected instead of onOptionsItemSelected and always return true. 覆盖onNavigationItemSelected而不是onOptionsItemSelected并始终返回true。

@Override public boolean onNavigationItemSelected(MenuItem item) { ... return true; @Override public boolean onNavigationItemSelected(MenuItem item){...返回true; } }

You are delegating onOptionsItemSelected() from the activity/fragment to the ActionBarDrawerToggle then consume the event by this: 您将onOptionsItemSelected()从活动/片段委派给ActionBarDrawerToggle然后按以下方式使用事件:

if(mToggle.onOptionsItemSelected(item))
{
     return true; //consume the event that handled by drawer toggle.
}

So you should implement how your drawer toggle react to the optionsItemSelected event by overriding it's own onOptionsItemSelected(MenuItem) . 因此,您应该通过覆盖抽屉本身的onOptionsItemSelected(MenuItem)来实现抽屉式开关对optionsItemSelected事件的反应。 See the code below: 请参见下面的代码:

mToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, R.string.open_drawer, R.string.close_drawer) {
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch(item.getItemId())
      {
           case R.id.new_flight: Log.d("SELECTED", "new_flight");
                break;
           case R.id.saved_flight: Log.d("SELECTED", "saved_flight");
                break;
           case R.id.settings: Log.d("SELECTED", "settings");
                break;
           default:
                break;
      }

      return super.onOptionsItemSelected(item);
   }
};

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

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