【问题标题】:multiple fragments in single tab (right now its fragment) in fragmentactivity片段活动中的单个选项卡中的多个片段(现在是它的片段)
【发布时间】:2013-02-16 06:22:30
【问题描述】:

我正在使用具有 5 个标签(片段)的 fragmentactivity。我需要的是第一个 tab1 有 Enter 按钮,点击它会切换到另一个片段在 tab1 中没有像现在这样得到任何奇怪的结果这就是我在 onClickListener 中所做的

FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.detach(Frag_Home.this);
ft.commit();                    
Frag_Locations newFragment = new Frag_Locations();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();


transaction.replace(android.R.id.content, newFragment);
transaction.addToBackStack(null);

transaction.commit();
getActivity().getSupportFragmentManager().executePendingTransactions();

**上面的代码改变了内容,但新添加的片段覆盖了整个视图,新打开的片段漂浮在选项卡上方!!

如何在tab1中添加fragmentactivity而不是fragment? (顺便说一句,我遵循 http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/ 教程)

编辑

tab1 => fragmentOne(onButtonClick 替换)=> fragmentTwo(点击)=>fragmentThree

tab2 => fragmentForth

tab3 => fragmentFifth

这就是我想要的

提前致谢

【问题讨论】:

  • 你有没有解决这个问题,因为我也被这个问题困住了。
  • @SathisKumar,我无法发布干净的答案,但知道我的结局非常干净。

标签: android tabs android-fragments android-fragmentactivity


【解决方案1】:

ActionBarSherlock's 示例:Fragments Example 中,这是一个非常好的示例,说明如何使用带有Fragments 的标签。看一看,我认为这是您想要实现的目标。 :)

【讨论】:

  • 我想我已经实现了几乎相同的功能,但使用了兼容性库..我可能问得不好..请检查编辑过的问题..谢谢
  • 您仍然可以在不使用 ABS 库的情况下使用示例中的此选项卡适配器。
  • 感谢您的帮助,但您能给我一个提示吗?我不明白你的意思(对不起,如果它冒犯了你!)谢谢
  • 您希望能够导航片段并将标签栏保留在右下角吗?所以更仔细地看一下代码:你有 TabManager 类,它实际上为你保存了片段。只需在您的 FragmentActivity 中添加一个侦听器并在某种事件上替换片段(例如 onButtonClick)。
  • 是的,我明白,试试这个例子,你会实现的。
【解决方案2】:

这就是我的结局..

人们可能知道在标签中添加片段

// HashMap for storing fragments so we could track them by their names
private HashMap mapTabInfo = new HashMap();

  // This one is the normal way to add fragment to tab (mine is customized so better look into some nice tutorial for better understading)
tabview = createTabView(mTabHost.getContext(), MY_TICKET, R.drawable.tab_icon_myticket_selector);
            MainTabActivity.addTab(this, this.mTabHost,
            this.mTabHost.newTabSpec(MY_TICKET).setIndicator(tabview),
            (tabInfo = new TabInfo(MY_TICKET, Frag_MyTicket.class, args)));

    this.mapTabInfo.put(tabInfo.tag, tabInfo);

    // Add these fragments here so we can track them by own interface's method 
    tabInfo = new TabInfo(LOCATIONS, Frag_Locations.class, args);
    this.mapTabInfo.put(LOCATIONS, tabInfo);

// These two fragments won't show up in tabs untill we make it display manually 
    tabInfo = new TabInfo(DAYS, Frag_Days.class, args);
    this.mapTabInfo.put(DAYS, tabInfo);

// Now when we want to show fragment LOCATION while showing My_TICKET fragment, we'll just call method
onTabChanged(String tag) // method called when tabs are tapped, call this method with string LOCATIONS and you are done there..

// I know this might not be easiest to understand, you'll understand the logic if you're familiar with fragments/tabs

【讨论】:

    猜你喜欢
    • 2016-08-13
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2012-10-08
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    相关资源
    最近更新 更多