我无法通过在android中使用TabHost单击选项卡来更改片段

时间:2014-09-30 06:17:32

标签: java android-fragments setter

我创建了一个带有三个片段并设置视图寻呼机的活动.....查看寻呼机工作正常,但是标签不起作用。选择tab时,它将在片段类中加载值但不显示...下面是ontabchange()

的代码
TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {

                @Override
                public void onTabChanged(String tabId) {
                    mFManager =   getSupportFragmentManager();
                    AndroidFragment androidFragment = (AndroidFragment) mFManager.findFragmentByTag("android");
                    AppleFragment appleFragment = (AppleFragment) mFManager.findFragmentByTag("apple");
                    MicrosoftFragment microsoftFragment = (MicrosoftFragment) mFManager.findFragmentByTag("microsoft");
                    mFTransaction = mFManager.beginTransaction();

                    if(androidFragment!=null)
                    {
                        mFTransaction.detach(androidFragment);
                    }

                    if(appleFragment!=null)
                    {
                        mFTransaction.detach(appleFragment);
                    }
                    if(microsoftFragment!=null)
                    {
                        mFTransaction.detach(microsoftFragment);
                    }

                    if(tabId.equalsIgnoreCase("android"))
                    {

                        if(androidFragment==null)
                        {
                            mFTransaction.add(R.id.tab1,new AndroidFragment(), "android");
                        }
                        else
                        {
                            mFTransaction.attach(androidFragment);
                        }
                    }
                    else if(tabId.equalsIgnoreCase("apple"))
                    {
                        if(appleFragment==null)
                        {
                            mFTransaction.add(R.id.tab1,new AppleFragment(), "apple");
                        }
                        else
                        {
                            mFTransaction.attach(appleFragment);
                        }
                    }
                    else if(tabId.equalsIgnoreCase("microsoft"))
                    {
                        if(microsoftFragment==null)
                        {
                            mFTransaction.add(R.id.tab1,new MicrosoftFragment(), "microsoft");
                        }
                        else
                        {
                            mFTransaction.attach(microsoftFragment);
                        }
                    }
                    else
                    {
                        if(androidFragment==null)
                        {
                            mFTransaction.add(R.id.tab1,new AndroidFragment(), "android");
                        }
                        else
                        {
                            mFTransaction.attach(androidFragment);
                        }
                    }
                    mFTransaction.commit();
                }
            };

            mTabHost.setOnTabChangedListener(tabChangeListener);
            TabHost.TabSpec tSpecAndroid = mTabHost.newTabSpec("android");
            tSpecAndroid.setIndicator("Android",getResources().getDrawable(R.drawable.android));
            tSpecAndroid.setContent(new FragmentControl(getBaseContext()));
            mTabHost.addTab(tSpecAndroid);

            TabHost.TabSpec tSpecApple = mTabHost.newTabSpec("apple");
            tSpecApple.setIndicator("Apple",getResources().getDrawable(R.drawable.apple));
            tSpecApple.setContent(new FragmentControl(getBaseContext()));
            mTabHost.addTab(tSpecApple);

            TabHost.TabSpec tSpecMicrosoft = mTabHost.newTabSpec("microsoft");
            tSpecMicrosoft.setIndicator("microsoft",getResources().getDrawable(R.drawable.microsoft));
            tSpecMicrosoft.setContent(new FragmentControl(getBaseContext()));
            mTabHost.addTab(tSpecMicrosoft);
            mTabHost.setCurrentTab(0);

请帮忙解决..提前致谢

1 个答案:

答案 0 :(得分:0)

经过长时间的斗争找到答案....... 想要将视图分页器和标签添加到带有片段的单独列表....答案是 单独的班级

  public class TabInformation 
{
public String tag;
private Class<?> clss;
private Bundle args;
private Fragment fragment;

TabInformation(String tag, Class<?> clazz, Bundle args) 
{
    this.tag = tag;
    this.clss = clazz;
    this.args = args;
}

}

MainActivity中的标签初始化

 private void TabInitialize(Bundle args) 
 {
    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();
    TabInformation tabInfo = null;
    MainActivity.AddTab(this,mTabHost, mTabHost.newTabSpec("Tab1").setIndicator("Andorid"), ( tabInfo = new TabInformation("Tab1", AndroidFragment.class, args)));
    mTabInfo.put(tabInfo.tag, tabInfo);
    MainActivity.AddTab(this, mTabHost, mTabHost.newTabSpec("Tab2").setIndicator("Apple"), ( tabInfo = new TabInformation("Tab2", AppleFragment.class, args)));
    mTabInfo.put(tabInfo.tag, tabInfo);
    MainActivity.AddTab(this, mTabHost, mTabHost.newTabSpec("Tab3").setIndicator("Microsoft"), ( tabInfo = new TabInformation("Tab3", MicrosoftFragment.class, args)));
    mTabInfo.put(tabInfo.tag, tabInfo);
    setSelectedTabColor();
 }

在MainActivity中查看寻呼机初始化

 private void ViewPagerInitialize()
 {
     List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, AndroidFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, AppleFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, MicrosoftFragment.class.getName()));
        mPageradapter  = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
        mViewPager = (ViewPager)super.findViewById(R.id.pager);
        mViewPager.setAdapter(mPageradapter);
        mViewPager.setOnPageChangeListener(this);
 }    

 private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInformation tabInfo) 
 {
        tabSpec.setContent(new FragmentControl(activity));
        tabHost.addTab(tabSpec);
  }