Fragment管理actionbar的menu——FragmentMenuSupport

2019-04-15 18:35发布

pic:
/** * Demonstrates how fragments can participate in the options menu. */ public class FragmentMenuSupport extends FragmentActivity { Fragment mFragment1; Fragment mFragment2; CheckBox mCheckBox1; CheckBox mCheckBox2; // Update fragment visibility when check boxes are changed. final OnClickListener mClickListener = new OnClickListener() { public void onClick(View v) { updateFragmentVisibility(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_menu); // Make sure the two menu fragments are created. FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); mFragment1 = fm.findFragmentByTag("f1"); mFragment2 = fm.findFragmentByTag("f2"); if (mFragment2 == null) { mFragment2 = new Menu2Fragment(); ft.add(mFragment2, "f2"); //不管是hide 还是show,都不会打破menuiem之间的顺序 //这个顺序是由add的顺序决定的~ } if (mFragment1 == null) { mFragment1 = new MenuFragment(); ft.add(mFragment1, "f1"); } ft.commit(); // Watch check box clicks. mCheckBox1 = (CheckBox) findViewById(R.id.menu1); mCheckBox1.setOnClickListener(mClickListener); mCheckBox2 = (CheckBox) findViewById(R.id.menu2); mCheckBox2.setOnClickListener(mClickListener); // Make sure fragments start out with correct visibility. updateFragmentVisibility(); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // Make sure fragments are updated after check box view state is restored. updateFragmentVisibility(); } // Update fragment visibility based on current check box state. void updateFragmentVisibility() { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); if (mCheckBox2.isChecked()) ft.show(mFragment2); else ft.hide(mFragment2); if (mCheckBox1.isChecked()) ft.show(mFragment1); else ft.hide(mFragment1); ft.commit(); } /** * A fragment that displays a menu. This fragment happens to not * have a UI (it does not implement onCreateView), but it could also * have one if it wanted. */ //这是一个没有UIview的fragment,但是依然可以管理actionbar添加每一个fragment想要的menuitem public static class MenuFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { MenuItem item; item = menu.add("Menu 1b"); MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); item = menu.add("Menu 1c"); MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); } } /** * Second fragment with a menu. */ public static class Menu2Fragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { MenuItem item; item = menu.add("Menu 1a"); MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); } } }