简体   繁体   English

带有片段的Android Actionbar选项卡

[英]Android Actionbar tabs with fragments

In my application I have a single activity that instantiates an ActionBar with three Fragment classes. 在我的应用程序中,我有一个活动来实例化具有三个Fragment类的ActionBar

// create action bar
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);

// set tabs
Tab tab = null;
tab = actionBar.newTab()
        .setIcon(R.drawable.tabbar_app_selected)
        .setTag(Globals.MainActivityTab.NewApp)
        .setTabListener(new MainActivityTabListener<BasicFormFragment>(this, "new_app_frag", BasicFormFragment.class, this));
actionBar.addTab(tab);

tab = actionBar.newTab()
        .setIcon(R.drawable.tabbar_saved)
        .setTag(Globals.MainActivityTab.Saved)
        .setTabListener(new MainActivityTabListener<SavedAppFragment>(this, "saved_apps_frag", SavedAppFragment.class, this));
actionBar.addTab(tab);

tab = actionBar.newTab()
        .setIcon(R.drawable.tabbar_settings)
        .setTag(Globals.MainActivityTab.Settings)
        .setTabListener(new MainActivityTabListener<SettingsFragment>(this, "settings_frag", SettingsFragment.class, this));
actionBar.addTab(tab);

During the runtime of my application there are times when I need to show a new fragment for a specific tab. 在我的应用程序运行期间,有时需要显示特定选项卡的新片段。 Todo this I add a new fragment via the FragmentManager like below. 为此,我通过FragmentManager如下添加一个新片段。 This is to emulate a drill down type of interface that one would find in any mobile app. 这是为了模拟在任何移动应用程序中都能找到的一种向下钻取的界面类型。

// show report screen
AppReportOptionsFragment reportOptionsFragment = new AppReportOptionsFragment();
reportOptionsFragment.SetAppraisal(_app);
reportOptionsFragment.SetIsNew(_isNew);

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.SavedAppFrame, reportOptionsFragment);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack("AppReportOptionsFragment");
fragmentTransaction.commit();

This code works just fine, and I see my fragment take place of the previous fragment. 这段代码很好用,我看到我的片段代替了前面的片段。 I also retain the ability to change tabs, and the tab changes is the issue. 我还保留了更改选项卡的功能,而选项卡更改就是问题所在。

Basically, the problem I'm having is when I instantiate a new fragment like above, and then change to a different tab then the previous tab is reset to the original fragment. 基本上,我遇到的问题是当我实例化如上的新片段,然后更改为其他选项卡,然后将先前的选项卡重置为原始片段。 Thus clearing out any work that the user may have done on the previous tab. 从而清除用户可能在上一个选项卡上所做的任何工作。

My question is how do I force the application to retain the previous tab's sub-fragment so that when I tap on the tab again then the user will see the tab as they left it. 我的问题是如何强制应用程序保留上一个选项卡的子片段,以便当我再次点击该选项卡时,用户在离开该选项卡时将看到该选项卡。 Basically, I want this to work exactly how UITabbarController in iOS works. 基本上,我希望它能够与iOS中的UITabbarController完全UITabbarController

Any suggestions on what am I doing wrong here, and is there a better way to implement a drill-down interface with Fragments and Actionbar tabs? 关于我在这里做错什么的任何建议,还有没有更好的方法用Fragments和Actionbar选项卡实现向下钻取接口?

Thanks! 谢谢!

This answer doesn't resolve your issue. 此答案不能解决您的问题。 It is only a warning about new API. 这只是有关新API的警告。 If you are using api 21 (it is not important if you are supporting old devices) you should consider to change your pattern because NAVIGATION_MODE_TABS is now deprecated. 如果您使用的是api 21(如果支持旧设备,这并不重要),则应考虑更改模式,因为NAVIGATION_MODE_TABS现在已弃用。

You can use a pattern like the SlidingTabLayout in GoogleIO app. 您可以在GoogleIO应用中使用类似SlidingTabLayout的模式。

Use Tab with new ToolBar (AppCompat v7-21) 将Tab与新工具栏一起使用(AppCompat v7-21)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM