onOptionsItemSelected未在Fragment中注册项目

时间:2017-10-28 06:43:50

标签: android android-fragments navigation-drawer

我有一个NavigationView的片段,它使用包含三个项目的菜单。但是,每当我单击菜单项时,都不会调用onOptionsItemSelected()函数。

这个NavigationView正在抽屉中使用,onOptionsItemSelected()功能在打开和关闭抽屉时起作用,而不是在点击菜单项时。

任何帮助都将不胜感激,谢谢。

片段类:

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

片段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>

4 个答案:

答案 0 :(得分:0)

覆盖片段中的OnCreateOptionsMenu。从你的片段代码来看,你似乎没有给片段中的菜单资源充气。

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

其中my_menu资源文件包含具有所需ID的项目

答案 1 :(得分:0)

1.在your_menu.xml文件夹中添加res/menu

2.添加onCreateOptionsMenu

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

答案 2 :(得分:0)

覆盖onNavigationItemSelected而不是onOptionsItemSelected,并始终返回true。

@覆盖 public boolean onNavigationItemSelected(MenuItem item){      ...      返回true; }

答案 3 :(得分:0)

您将onOptionsItemSelected()从活动/片段委派给ActionBarDrawerToggle,然后通过此方式使用该事件:

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

因此,您应该通过覆盖它自己的optionsItemSelected来实现抽屉切换对onOptionsItemSelected(MenuItem)事件的反应。请参阅以下代码:

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