在操作栏选项卡片段的片段? [英] Fragments in Action Bar tab fragments?

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

问题描述

您可以把片段的片段里面的操作栏标签?

Can you put fragments inside the fragment for a tab in the Action Bar?

我有一款Android(3.0 /蜂窝)应用程序,有一个操作栏3个选项卡的主要活动。标签被添加在我的活动的onCreate()方法,以及标签的片段被添加/带TabListener除去。在code是几乎相同的样品在<一个href="http://developer.android.com/guide/topics/ui/actionbar.html#Tabs">http://developer.android.com/guide/topics/ui/actionbar.html#Tabs.

I have a Android (3.0/Honeycomb) application with a main activity that has a Action Bar with 3 tabs. The tabs are added in my activity's onCreate() method, and the tab fragments are added/removed with a TabListener. The code is nearly identical to the sample at http://developer.android.com/guide/topics/ui/actionbar.html#Tabs.

在TabListener看起来是这样的:

The TabListener looks like this:

public class SwapOutTabListener implements ActionBar.TabListener {
    public SwapOutTabListener(Fragment fragment) {
        _fragment = fragment;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.add(R.id.fragment_container, _fragment, null);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(_fragment);
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // do nothing
    }

    private Fragment _fragment;
}

我的两个标签是简单的片段,它们只包含一个TextView的在其中,有点像这样(大多数属性为清楚起见):

Two of my tabs are simple fragments, they just contain a single TextView in them, somewhat like this (most attributes removed for clarity):

<LinearLayout>
    <TextView android:text="Tab 1" />
</LinearLayout>

但片段我的标签之一就是更加复杂,并包含两个嵌入式片段,有点像这样的:

But the fragment for one of my tabs is more complicated, and contains two embedded fragments, kinda like this:

<LinearLayout>
    <fragment
        android:name="...Fragment_1"
        android:id="@+id/frag1"
    />
    <fragment
        android:name="...Fragment_2"
        android:id="@+id/frag2"
    />
</LinearLayout>

当用户选择的标签进行该片段中,所有的启动生命周期方法(ONSTART(),onResume())被调用的所有三个片段(的标签片段,加上两个嵌入式片段)。

When the user selects the tab for this fragment, all of the start-up lifecycle methods (onStart(), onResume()) get called for all three fragments (the tab fragment, plus the two embedded fragments).

但是,当用户随后选择另一个选项卡,只有标签片段得到任何的生命周期终止的方法(的onPause(),的onStop(),等等)。这两个嵌入式片段从来没有得到任何这些调用,并且从不关闭。

But when the user then selects another tab, only the tab fragment gets any of the end-of-lifecycle methods (onPause(), onStop(), etc.). The two embedded fragments never get any of these calls and are never shut down.

这会导致问题,当标签被重新选择,重复片段ID作为加载选项卡中的片段时,运行时抱怨:

This causes problems when the tab is re-selected, as the runtime complains of a duplicate fragment ID when loading the tab fragment:

Binary XML file line #7: Duplicate id 0x7f05000a, tag null, or parent id 0x7f050009 with another fragment for ...Fragment_1

这是我的责任,以消除这些嵌入式片段时,片片段被删除?如果是这样的时候,正好,我该怎么办呢?

Is it my responsibility to remove these embedded fragments when the tab fragment is removed? If so, when, exactly, do I do that?

推荐答案

没有,片段当前不在一个层次。我看着这样做,但在这一点上所有的用例为它被赶出大多由一个极端过度使用的片段,其中每个单独的UI元素是实现一个片段。这不是它们打算如何使用,它们用于封装的主要顶层片的应用程序。如果你有东西一个层次,这就是你的布局和视图的。

No, fragments are currently not in a hierarchy. I looked at doing this, but at this point all of the use cases for it have been driven mostly by an extreme over-use of fragments where each separate UI element is implement as a fragment. That isn't how they are intended to be used, they are intended to encapsulate the major top-level pieces of an application. If you have a hierarchy of stuff, that is what your layout and views are for.

这篇关于在操作栏选项卡片段的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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