【问题标题】:Change Tab Programmatically from Fragment with FragmentPagerAdapter使用 FragmentPagerAdapter 以编程方式从 Fragment 更改选项卡
【发布时间】:2014-08-20 15:14:57
【问题描述】:

我正在尝试让我的应用从标签 1 更改为标签 3。标签位于自定义 TabsPagerAdapter 中,其中 extends FragmentPagerAdapter

我尝试将标签更改为这样,但它会导致NullPointerException。机制与FragmentPagerAdapter 不同吗?

TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
host.setCurrentTab(2);

【问题讨论】:

    标签: java android android-fragments android-tabhost fragmentpageradapter


    【解决方案1】:

    知道了。需要使用我的ViewPager 而不是TabHost

    ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
    viewPager.setCurrentItem(2);
    

    【讨论】:

      【解决方案2】:

      将此代码用于标签

      public class MainActivity extends FragmentActivity implements TabHost.OnTabChangeListener {
      
          private TabHost mTabHost;
          private final HashMap<String, TabInfo> mapTabInfo = new HashMap<String, MainActivity.TabInfo>();
          TabInfo mLastTab = null;
          public Context mContext;
          public MainActivity mainActivity;
          static IViewFragmentListener fragmentActionListner;
          String[] menuItems;
          int[] menuItemsIcons;
      
          private class TabInfo {
              private final String tag;
              private final Class<?> clss;
              private final Bundle args;
              private Fragment fragment;
      
              TabInfo(String tag, Class<?> clazz, Bundle args) {
                  this.tag = tag;
                  this.clss = clazz;
                  this.args = args;
              }
      
          }
      
          class TabFactory implements TabContentFactory {
      
              private final Context mContext;
      
              public TabFactory(Context context) {
                  mContext = context;
              }
      
              @Override
              public View createTabContent(String tag) {
                  View v = new View(mContext);
                  v.setMinimumWidth(0);
                  v.setMinimumHeight(0);
                  return v;
              }
      
          }
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              setContentView(R.layout.am_tabs_layout);
      
      
              mContext = this;
              mainActivity = this;
              initialiseTabHost(savedInstanceState);
      
      
      
          }
      
          private void initialiseTabHost(Bundle args) {
              mTabHost = (TabHost) findViewById(android.R.id.tabhost);
              mTabHost.setup();
              TabInfo tabInfo = null;
              MainActivity.addTab(
                      this,
                      this.mTabHost,
                      this.mTabHost.newTabSpec("Tab1").setIndicator("Name of your 1st tab",
                              getResources().getDrawable(R.drawable.running)),
                      (tabInfo = new TabInfo("Tab1", NextActivity.class, args)));
              this.mapTabInfo.put(tabInfo.tag, tabInfo);
              MainActivity.addTab(
                      this,
                      this.mTabHost,
                      this.mTabHost.newTabSpec("Tab2").setIndicator("Name of your 2nd tab",
                              getResources().getDrawable(R.drawable.xyz)),
                      (tabInfo = new TabInfo("Tab2", NextActivity.class, args)));
              this.mapTabInfo.put(tabInfo.tag, tabInfo);
              MainActivity.addTab(
                      this,
                      this.mTabHost,
                      this.mTabHost.newTabSpec("Tab3").setIndicator("Name of your 3rd tab",
                              getResources().getDrawable(R.drawable.abc)),
                      (tabInfo = new TabInfo("Tab3", NextActivity.class, args)));
              this.mapTabInfo.put(tabInfo.tag, tabInfo);
              this.onTabChanged("Tab1");
              mTabHost.setOnTabChangedListener(this);
          }
      
          private static void addTab(MainActivity activity, TabHost tabHost,
                  TabHost.TabSpec tabSpec, TabInfo tabInfo) {
      
              tabSpec.setContent(activity.new TabFactory(activity));
              String tag = tabSpec.getTag();
      
              if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
                  FragmentTransaction ft = activity.getSupportFragmentManager()
                          .beginTransaction();
                  ft.detach(tabInfo.fragment);
                  ft.commit();
                  activity.getSupportFragmentManager().executePendingTransactions();
              }
              tabHost.addTab(tabSpec);
      
          }
      
          public void onTabChanged(String tag) {
              TabInfo newTab = this.mapTabInfo.get(tag);
              if (mLastTab != newTab) {
                  FragmentTransaction ft = this.getSupportFragmentManager()
                          .beginTransaction();
                  if (mLastTab != null) {
                      if (mLastTab.fragment != null) {
                          ft.detach(mLastTab.fragment);
                      }
                  }
                  if (newTab != null) {
                      if (newTab.fragment == null) {
                          newTab.fragment = Fragment.instantiate(this,
                                  newTab.clss.getName(), newTab.args);
                          ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
                      } else {
                          ft.attach(newTab.fragment);
                      }
                  }
      
                  mLastTab = newTab;
                  fragmentActionListner = ((IViewFragmentListener) mLastTab.fragment);
                  ft.commit();
                  this.getSupportFragmentManager().executePendingTransactions();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-06-01
        • 2021-12-08
        • 1970-01-01
        • 2016-12-03
        • 1970-01-01
        • 2021-08-13
        • 2011-06-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多