2012-06-29 53 views
2

雖然我意識到,嵌套的片段是不是一種選擇,我仍然有我簡直想不出回答的問題。如何更換從一個TabAdapter一個選項卡中的片段?

我使用ActionBarSherlock的FragmentsTabPager例如創建一個界面,就可以通過網頁的標籤與刷卡,而不是點擊選項卡。我的問題是這些選項卡中的一個包含listview。點擊該列表視圖,包含一個新的列表(基於項目數據點擊)不同的片段推出。我如何做到這一點?

我想你可能需要一個位COSE從我BaseActivity(那個以ViewPager和TabsAdapter

*snippet* 

public class BaseActivity extends SherlockFragmentActivity { 
TabHost mTabHost; 
ViewPager mViewPager; 
TabsAdapter mTabsAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.fragment_tabs_pager); 
    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("home").setIndicator("Home"), 
      FragmentStackSupport.CountingFragment.class, null); 
    mTabsAdapter.addTab(mTabHost.newTabSpec("players").setIndicator("PLAYERS"), 
      PlayerRankingFragment.class, null); 
    mTabsAdapter.addTab(mTabHost.newTabSpec("teams").setIndicator("TEAMS"), 
      FederationRanksFragment.class, null); 

    if (savedInstanceState != null) { 
     mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); 
    } 
} 

/** 
* This is a helper class that implements the management of tabs and all 
* details of connecting a ViewPager with associated TabHost. It relies on a 
* trick. Normally a tab host has a simple API for supplying a View or 
* Intent that each tab will show. This is not sufficient for switching 
* between pages. So instead we make the content part of the tab host 
* 0dp high (it is not shown) and the TabsAdapter supplies its own dummy 
* view to show as the tab content. It listens to changes in tabs, and takes 
* care of switch to the correct paged in the ViewPager whenever the selected 
* tab changes. 
*/ 
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>(); 

    static final class TabInfo { 
     private final String tag; 
     private final Class<?> clss; 
     private final Bundle args; 

     TabInfo(String _tag, Class<?> _class, Bundle _args) { 
      tag = _tag; 
      clss = _class; 
      args = _args; 
     } 
    } 
*Snippet* 

BaseActivity - >選項卡 「團隊」 - >團隊的ListView(AllianceFragment) - >團隊。項目已點擊 - >玩家的ListView(PlayersFragment)

最後一點,這裏的應用程序的問題截圖:

https://dl.dropbox.com/u/11917448/Screenshot_2012-06-28-13-32-21.png

+0

你能告訴我們你的PlayerRankingFragment.class和xml佈局嗎? – Jeroen

回答

0

BTW,現在最新的支持庫,分片嵌套支持剛剛加入。請參閱getChildFragmentManager()方法。但要小心內存泄漏。我仍然在玩他們。我使用它與PagerTabStrip和ViewPager。效果很好。

相關問題