2013-03-20 2 views
-1

주 FragmentActivity에서 TabHost를 사용하는 새로운 FragmentActivity를 표시하는 샘플 코드를 얻고 싶습니다. FragmentActivity는 5 개의 탭으로 구성되며 탭 중 하나에는 탭이있는 새 활동이 표시되어야합니다. 내 코드 : 다른TabHost에서 탭으로 단편화 작업

 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();     
     } 
    }; 

내부 경우 (tabId.equalsIgnoreCase는 ("검색")) 내가 다음 FragmentActivity 가진 tabhost를 표시하는 지연 있도록 다음 활동으로 이동하는 의도를 사용하는 위의 코드이다. 지연없이 활동을 표시하고 싶습니다.

+0

그래서 TEH 문제가 : 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); } 

사용하여 각 탭을 추가? –

+0

새로운 FragmentActivity를 탭에 추가하는 코드를 얻고 싶습니다. –

+0

[FAQ를 읽어야합니다.] (http://stackoverflow.com/faq) 그들은 여기에서 질문하는 방법과 질문 할 질문을 더 잘 이해할 수 있도록 도와줍니다. – adneal

답변

0

MainActivity가 있어야하며 tabHost에 맞게 어댑터를 설정해야합니다. 이 어댑터는 FragmentPageAdapter를 확장합니다.

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(); 
    } 
}