-1

メインの FragmentActivity で TabHost を使用する新しい FragmentActivity を表示するサンプル コードを取得したいと考えています。FragmentActivity は 5 つのタブで構成され、タブの 1 つは、タブを持つ新しいアクティビティを表示する必要があります。私のコード:

        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")) 内で、インテントを使用して次のアクティビティに移動したため、タブホストを持つ次の FragmentActivity の表示が遅れました。アクティビティを遅延なく表示したい。

4

1 に答える 1

0

MainActivity があり、tabHost の Adapter を設定する必要があります。このアダプターは 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 に答える