0

我试图弄清楚为什么在我的主要活动中的片段之间切换时菜单在操作栏中没有改变。我已经尝试了一些关于 setHasOptionsMenu(true) 和 menu.clear 的建议,但没有运气......我只是不明白。

ConsoleActivity.class

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  // Set up the action bar to show a dropdown list.
  final ActionBar actionBar = getActionBar();
  actionBar.setDisplayShowTitleEnabled(false);
  actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

  // Set up the dropdown list navigation in the action bar.
  actionBar.setListNavigationCallbacks(

  // Specify a SpinnerAdapter to populate the dropdown list.
  new ArrayAdapter<String>(actionBar.getThemedContext(),
    android.R.layout.simple_list_item_1,
    android.R.id.text1, new String[] {
    getString(R.string.NavItem1),
    getString(R.string.NavItem2) }), this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
}


@Override
public boolean onNavigationItemSelected(int position, long id) {
  // When the given dropdown item is selected, show its contents in the
  // container view.
  Fragment fragment = new SectionFragment();
  Bundle args = new Bundle();
  args.putInt(SectionFragment.ARG_SECTION_NUMBER, position + 1);
  fragment.setArguments(args);
  getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
  return true;
}


public static class SectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";

public SectionFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  // Create a new TextView and set its text to the fragment's section
  // number argument value.
  View mView = null;
  switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
  case 1:
    mView = (View) inflater.inflate(R.layout.schedule_fragment, container, false);
    break;
  case 2:
    mView = (View) inflater.inflate(R.layout.timesheet_fragment, container, false);
    break;
  }         
  return mView;
}

ScheduleFragment.class:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

public class ScheduleFragment extends Fragment {

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

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    return inflater.inflate(R.layout.schedule_fragment, null);
  }

  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.schedule_context, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

}

菜单文件夹中的 schedule.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/menu_edit_trips"
          android:icon="@drawable/ic_menu_trips"
          android:title="Trips"
          android:showAsAction="always|withText">
        <menu>
            <item android:id="@+id/menuitem_trip_edit"
                  android:icon="@drawable/ic_menu_trip_edit"
                  android:title="Edit" />

            <item android:id="@+id/menuitem_trip_new"
                  android:icon="@drawable/ic_menu_trip_new"
                  android:title="New" />

            <item android:id="@+id/menuitem_trip_cancel"
                  android:icon="@drawable/ic_menu_trip_cancel"
                  android:title="Cancel" />
        </menu> 
    </item>

</menu>

菜单文件夹中的 main.xml。这是在所有片段之间保持一致的菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/menuitem_main"
          android:icon="@drawable/ic_menu_main"
          android:title="Main"
          android:showAsAction="always">
        <menu>
            <item
                android:id="@+id/menuitem_logout"
                android:icon="@drawable/ic_menu_main_logout"
                android:title="Logout" />

            <item
                android:id="@+id/menuitem_about"
                android:icon="@drawable/ic_menu_main_about"
                android:title="About" />

            <item
                android:id="@+id/menuitem_settings"
                android:icon="@drawable/ic_menu_main_settings"
                android:title="@string/menu_settings" />
        </menu>
    </item>

</menu>

我不知道该怎么办...

4

1 回答 1

0

我想我明白了,不确定这是否是正确的解决方案,但这就是我所做的:

我将此添加到我的 ConsoleActivity.class

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuInflater inflater = new MenuInflater(this);
    switch (sectionNumber) {
    case 1:
        inflater.inflate(R.menu.schedule, menu);
        break;
    case 2:
        inflater.inflate(R.menu.timesheet, menu);
        break;      
    default:
        menu.clear();
        inflater.inflate(R.menu.main, menu);
        break;
    }
    return true;
}

我添加了一个类变量:

private byte sectionNumber = 0;

最后我添加了 getActivity().invalidateOptionsMenu(); 这里:

public static class SectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public SectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        View mView = null;
        switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
        case 1:
            mView = (View) inflater.inflate(R.layout.schedule_fragment, container, false);
            break;
        case 2:
            mView = (View) inflater.inflate(R.layout.timesheet_fragment, container, false);
            break;
        }
        getActivity().invalidateOptionsMenu();
        return mView;
    }       
}

这是我的 onNavigationItemSelected:

@Override
public boolean onNavigationItemSelected(int position, long id) {
    // When the given dropdown item is selected, show its contents in the
    // container view.
    Fragment fragment = new SectionFragment();
    Bundle args = new Bundle();
    sectionNumber = (byte) (position + 1);
    args.putInt(SectionFragment.ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
    return true;
}
于 2013-02-13T20:15:33.900 回答