如何从一个TabAdapter一个选项卡替换片段? [英] How do I replace the fragment from one of the tabs in a TabAdapter?

查看:214
本文介绍了如何从一个TabAdapter一个选项卡替换片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While I realize that nested fragments is not an option i still have an issue that I simply cannot figure out an answer to.

我使用ActionBarSherlock的FragmentsTabPager例如创建一个界面,就可以通过网页的标签与刷卡,而不是点击标签。我的问题是,这些标签之一包括列表视图中。点击该列表视图,包含一个新的列表(基于项目数据点击)不同的片段启动。如何我做到这一点?

I'm using ActionBarSherlock's FragmentsTabPager example to create an interface where it is possible to page through the tabs with swiping rather than clicking on the tabs. My problem is that one of these tabs consists of a listview. When this listview is clicked, a different fragment containing a new list(with data based on the item clicked) is launched. How to I accomplish this ?

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

I figured you might need a bit of the cose from my BaseActivity(The one with the ViewPager and the 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)。

BaseActivity -> Tab "Teams" -> Teams ListView(AllianceFragment) -> Teams Item Clicked -> Players ListView(PlayersFragment).

和最后这里是有问题的应用程序的截图:

And lastly here's a screenshot of the app in question:

推荐答案

顺便说一句,现在最新的支持库,嵌套的片段支持刚刚加入。见getChildFragmentManager()方法。但要小心内存泄漏。我仍然玩弄他们。我PagerTabStrip和ViewPager用它。效果很好。

BTW, now with latest support library, nested fragment support just got added. See getChildFragmentManager() method. But be careful about memory leaks. I am still playing around with them. I used it with PagerTabStrip and ViewPager. Works well.

这篇关于如何从一个TabAdapter一个选项卡替换片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆