2

虽然我意识到嵌套片段不是一个选项,但我仍然有一个我根本无法找到答案的问题。

我正在使用 ActionBarSherlock 的 FragmentsTabPager 示例来创建一个界面,在该界面中,可以通过滑动而不是单击选项卡来翻页。我的问题是这些选项卡之一包含一个列表视图。单击此列表视图时,将启动包含新列表(具有基于单击的项目的数据)的不同片段。我该如何做到这一点?

我想你可能需要我的 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 -> 选项卡“Teams” -> Teams ListView(AllianceFragment) -> Teams Item Clicked -> Players ListView(PlayersFragment)。

最后是相关应用的截图:

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

4

1 回答 1

0

顺便说一句,现在有了最新的支持库,刚刚添加了嵌套片段支持。请参阅 getChildFragmentManager() 方法。但要小心内存泄漏。我还在和他们玩。我将它与 PagerTabStrip 和 ViewPager 一起使用。效果很好。

于 2013-01-10T22:06:42.207 回答