コード例 #1
0
 public TabsAdapter(MainActivity activity, TabHost tabHost, ViewPager pager) {
   super(activity.getSupportFragmentManager());
   mContext = activity;
   mTabHost = tabHost;
   mViewPager = pager;
   mTabHost.setOnTabChangedListener(this);
   mViewPager.setAdapter(this);
   mViewPager.setOnPageChangeListener(this);
   mainActivity = activity;
 }
コード例 #2
0
    @Override
    public void onTabChanged(String tabId) {
      int position = mTabHost.getCurrentTab();

      Log.i("ll", "" + position);
      /**
       * TabInfo newTab = mTabs.get(position); if (mLastTab != newTab) { if (mLastTab != newTab) {
       * FragmentTransaction ft = mainActivity.getSupportFragmentManager().beginTransaction();
       *
       * <p>if (newTab != null) { if (newTab.fragment == null) { newTab.fragment =
       * Fragment.instantiate(mainActivity, newTab.clss.getName(), newTab.args);
       * ft.add(newTab.fragment, newTab.tag); } else { ft.replace(newTab.fragment, newTab.tag);
       *
       * <p>ft.attach(newTab.fragment); } } mLastTab = newTab; ft.commit(); mainActivity
       * .getSupportFragmentManager().executePendingTransactions(); } }
       */
      TabInfo newTab = mTabs.get(position);

      if (newTab != null) {}

      /**
       * if (mLastTab != newTab) {
       *
       * <p>if (mLastTab != null) { if (mLastTab.fragment != null) {
       *
       * <p>if (mLastTab.fragment instanceof ContactsFragment) { FragmentTransaction ft =
       * mainActivity.getSupportFragmentManager().beginTransaction(); ft.SupportMapFragment map =
       * ((ContactsFragment) mLastTab.fragment).getGoogleMap(); ft.hide(map); ft.commit(); } //
       * ft.detach(mLastTab.fragment);
       *
       * <p>} }
       *
       * <p>/** if (newTab != null) { if (newTab.fragment == null) { newTab.fragment =
       * getItem(position); ft.add(newTab.fragment, newTab.tag); } else {
       * ft.attach(newTab.fragment); } }
       */

      // mLastTab = newTab;
      // }

      try {

        for (int i = 0; i < mTabs.size(); i++) {
          if (i == position) {
            continue;
          }
          TabInfo tab = mTabs.get(i);
          if (tab.fragment instanceof ContactsFragment) {

            SupportMapFragment map = ((ContactsFragment) tab.fragment).getGoogleMap();
            if (map != null) {
              FragmentTransaction ft = mainActivity.getSupportFragmentManager().beginTransaction();
              ft.remove(map);
              ft.commit();
            }
          }
        }
      } catch (Exception ex) {

      }

      mViewPager.setCurrentItem(position);

      if (mainActivity.menu != null) {
        mainActivity
            .menu
            .findItem(R.id.refresh_button)
            .setVisible(newTab.fragment instanceof IRefreshFragment);
      }
    }