片段菜单没有变化

时间:2013-02-13 05:16:35

标签: android android-fragments android-menu

我想弄清楚为什么在我的主要活动中切换片段时,操作栏中的菜单没有变化。我已经尝试了一些关于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>

我不知道该怎么做......

2 个答案:

答案 0 :(得分:1)

也许this链接可以解决您的问题?

答案 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;
}