-1

我想获得一个示例代码来显示一个在我的主 FragmentActivity 中使用 TabHost 的新 FragmentActivity。FragmentActivity 由五个选项卡组成,其中一个选项卡必须显示具有选项卡的新活动。我的代码:

        TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {




             fm =   getSupportFragmentManager();
             venueFragment = (FragmentVenue) fm.findFragmentByTag("venue");
             favoriteFragment = (FragmentFavorite) fm.findFragmentByTag("favorite");
             venueTrendingFragment = (FragmentVenueTrending) fm.findFragmentByTag("venuetrendingnow");
             searchFragment = (FragmentSearch) fm.findFragmentByTag("search");
             accountFragment = (FragmentAccount) fm.findFragmentByTag("account");



            android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();

            /** Detaches the venueFragment if exists */
            if(venueFragment!=null)
                ft.detach(venueFragment);

            /** Detaches the favoriteFragment if exists */
            if(favoriteFragment!=null)
                ft.detach(favoriteFragment);

            /** Detaches the venueTrendingFragment if exists */
            if(venueTrendingFragment!=null)
                ft.detach(venueTrendingFragment);

            /** Detaches the searchFragment if exists */
            if(searchFragment!=null)
                ft.detach(searchFragment);

            if(accountFragment!=null)
                ft.detach(accountFragment);

            /** If current tab is venue */
            if(tabId.equalsIgnoreCase("venue")){                

                if(venueFragment==null){        
                    /** Create venueFragment and adding to fragment transaction */
                    ft.add(R.id.realtabcontent,new FragmentVenue(), "venue");                       
                }else{
                    /** Bring to the front, if already exists in the fragment transaction */
                    ft.attach(venueFragment);                       
                }

            }else if(tabId.equalsIgnoreCase("favorite")){   /** If current tab is favorite */

                if(favoriteFragment==null){
                    /** Create favoriteFragment and adding to fragment transaction */
                    ft.add(R.id.realtabcontent,new FragmentFavorite(), "favorite");                     
                }else{
                    /** Bring to the front, if already exists in the fragment transaction */
                    ft.attach(favoriteFragment);                        
                }
            }else if(tabId.equalsIgnoreCase("venuetrendingnow")){   /** If current tab is venueTrendingFragment */

                if(venueTrendingFragment==null){
                    /** Create venueTrendingFragment and adding to fragment transaction */
                    ft.add(R.id.realtabcontent,new FragmentVenueTrending(), "venuetrendingnow");                        
                }else{
                    /** Bring to the front, if already exists in the fragment transaction */
                    ft.attach(venueTrendingFragment);                       
                }
            }else if(tabId.equalsIgnoreCase("search")){ /** If current tab is searchFragment */

                if(searchFragment==null){

                    /** Create searchFragment and adding to fragment transaction */
                    //ft.add(R.id.realtabcontent,new FragmentSearch(MainActivity.this), "search");
                    //Intent intent = new Intent(this,SecondTabhost.class);
                    Intent intent = new Intent(MainActivity.this, SecondTabhost.class);
                    startActivity(intent); 
                }else{
                    /** Bring to the front, if already exists in the fragment transaction */
                    //ft.attach(searchFragment);    
                    Intent intent = new Intent(MainActivity.this, SecondTabhost.class);
                    startActivity(intent);
                }
            }else if(tabId.equalsIgnoreCase("account")){    /** If current tab is accountFragment */

                if(accountFragment==null){
                    /** Create accountFragment and adding to fragment transaction */
                    ft.add(R.id.realtabcontent,new FragmentAccount(), "account");                       
                }else{
                    /** Bring to the front, if already exists in the fragment transaction */
                    ft.attach(accountFragment);                     
                }
            }

            ft.commit();                
        }
    };

在上面代码中的 else if(tabId.equalsIgnoreCase("search")) 内部,我使用 Intent 转到下一个活动,因此延迟显示下一个具有 tabhost 的 FragmentActivity。我想毫不拖延地显示活动。

4

1 回答 1

0

您必须有一个 MainActivity 并为您的 tabHost 设置一个适配器。此适配器扩展了 FragmentPageAdapter。在 MainActivity.onCreate 添加每个选项卡使用

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mViewPager = (ViewPager) findViewById(R.id.pager);

    mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
    mTabsAdapter.addTab(mTabHost.newTabSpec("form"), YourFragment.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("form"), AnotherFragment.class, null);
    }

在适配器中:

public static class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener,
        ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final TabHost mTabHost;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    public static final class TabInfo {
        private final String tag;
        private final Fragment fragment;
        private final Bundle args;

        TabInfo(String _tag, Fragment _fragment, Bundle _args) {
            tag = _tag;
            fragment = _fragment;
            args = _args;
        }

        public Fragment getFragment() {
            return fragment;
        }
    }

    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context mContext;

        public DummyTabFactory(Context context) {
            mContext = context;
        }

        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }


    public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mTabHost = tabHost;
        mViewPager = pager;
        mTabHost.setOnTabChangedListener(this);
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);

    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
        tabSpec.setContent(new DummyTabFactory(mContext));
        String tag = tabSpec.getTag();

        Fragment fragment = Fragment.instantiate(mContext, clss.getName(), args);
        TabInfo info = new TabInfo(tag, fragment, args);
        mTabs.add(info);
        mTabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

    public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mTabHost = tabHost;
        mViewPager = pager;
        mTabHost.setOnTabChangedListener(this);
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
        tabSpec.setContent(new DummyTabFactory(mContext));
        String tag = tabSpec.getTag();

        Fragment fragment = Fragment.instantiate(mContext, clss.getName(), args);
        TabInfo info = new TabInfo(tag, fragment, args);
        mTabs.add(info);
        mTabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return info.getFragment();
    }
}
于 2013-03-20T10:29:21.700 回答