片段更改时更改活动菜单

时间:2015-03-06 10:15:39

标签: android

我在我的Android应用中使用MainActivity和PagerSlidingTabStrip库的ViewPager有三个标签。

问题是如何在片段发生变化时更改MainActivity的菜单(即Tab更改)。

每个标签都有不同的菜单。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

您应该在ViewPager中使用的每个片段中覆盖 onCreateOptionsMenu ,并在扩充菜单资源之前使用menu.clear()。

您可以在此link

中查看如何使用此功能

尝试"在运行时更改菜单项"章。

答案 1 :(得分:0)

在我的情况下,我正在使用2个片段......你只是要添加一个

  1. 片段

    ...

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
    
        final View rootView = inflater.inflate(R.layout.login, container, false);
    

    ...

  2. 它连接到布局 登录

      除了布局

      之外,
    1. 片段基本相同

      ...     public View onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){

          View rootView = inflater.inflate(R.layout.register, container, false);
      ...
      

      它连接到布局 注册

    2. 然后我们有TabsPagerAdapter

      public class TabsPagerAdapter extends FragmentPagerAdapter {
      
          public TabsPagerAdapter(FragmentManager fm)
          {
              super(fm);
          }
      
          @Override
          public Fragment getItem(int index) {
      
              switch (index) {
              case 0:
                  // 1 fragment activity
                  return new LoginFragment();
              case 1:
                  // 2 fragment activity
                  return new RegisterFragment();
      
              }
      
              return null;
          }
      
          @Override
          public int getCount() {
              // get item count - equal to number of tabs
              return 2;
          }
      
      }
      

      你在MainActivity中所称的所有内容

          public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener,ActionBar.TabListener {
      
          private ViewPager mViewPager;
          private TabsPagerAdapter mAdapter;
          private TabHost mTabHost;
          private String[] tabs = { "1.Fragment", "2.Fragment"};
      
          private TabHost.TabContentFactory mTabFactory = new TabHost.TabContentFactory() {
              @Override
              public View createTabContent(String tag) {
                  View v = new View(MainActivity.this);
                  v.setMinimumHeight(0);
                  v.setMinimumWidth(0);
                  return v;
              }
          };
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              setContentView(R.layout.view_bottomtabs);
              mViewPager = (ViewPager) findViewById(R.id.viewPager);
              mViewPager.setOnPageChangeListener(this);
      
              mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
              mViewPager.setAdapter(mAdapter);
              mTabHost = (TabHost) findViewById(android.R.id.tabhost);
              mTabHost.setup();
              mTabHost.setOnTabChangedListener(this);
              for (String tab_name : tabs){
                  TabHost.TabSpec spec =mTabHost.newTabSpec(tab_name);
                  spec.setIndicator(tab_name);
                  spec.setContent(mTabFactory);
                  mTabHost.addTab(spec);
              }
      
          }
      
      
      
          @Override
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
      
          }
      
          @Override
          public void onPageSelected(int position) {
              mTabHost.setCurrentTab(position);
          }
      
          @Override
          public void onPageScrollStateChanged(int state) {
      
          }
      
          @Override
          public void onTabChanged(String tabId) {
              int position = -1;
              if (tabId.contains("1.Fragment")) {
                  position = 0;
              } else if (tabId.contains("2.Fragment")) {
                  position = 1;
              }
              mViewPager.setCurrentItem(position);
          }
      
          @Override
          public void onTabReselected(Tab tab, FragmentTransaction ft) {
          }
      
          @Override
          public void onTabSelected(Tab tab, FragmentTransaction ft) {
              // on tab selected
              // show respected fragment view
              mViewPager.setCurrentItem(tab.getPosition());
          }
      
          @Override
          public void onTabUnselected(Tab tab, FragmentTransaction ft) {
          }
      
      
      }
      

      我希望这有帮助