How do I replace the fragment from one of the tabs

2019-05-26 01:01发布

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

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 ?

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 -> Tab "Teams" -> Teams ListView(AllianceFragment) -> Teams Item Clicked -> Players ListView(PlayersFragment).

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

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

1条回答
等我变得足够好
2楼-- · 2019-05-26 01:36

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.

查看更多
登录 后发表回答