里面ActivityTab的Andr​​oid MasterDetail布局避免嵌套的片段嵌套、片段、布局、里面

2023-09-12 03:21:58 作者:22.笑低了眉眼

我不是很精通Android的碎片。在我的最后一个项目我使用TabActivity,但由于它是德precation现在我已经开始实现使用片段的动作条。

I'm not very expert in android's Fragment. In my last project i was using TabActivity, but because of it's deprecation now I'm starting implements an ActionBar using Fragments.

下面是我的标签类的code:

Here is the code of my Tab Class:

public class TabInterventoClass extends FragmentActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(false);

    Tab tab = actionBar.newTab()
            .setText("Dati")
            .setTabListener(new TabListener<DatiFragment_>(this, "dati", DatiFragment_.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
            .setText("Sistemi alimentazione")
            .setTabListener(new TabListener<SistemaAlimentazioneFragment_>(this, "Sistemi alimentazione", SistemaAlimentazioneFragment_.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
        .setText("Home")
        .setTabListener(new TabListener<HomeFragment_>(this, "home", HomeFragment_.class));
    actionBar.addTab(tab);
}

我在这种方式实现ActionBar.TabListener:

I've implemented ActionBar.TabListener in this way:

public class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    private final FragmentActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    public TabListener(FragmentActivity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            //ft.attach(mFragment);
            ft.show(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            //ft.detach(mFragment);
            ft.hide(mFragment);
        }
    }
}

请注意在安装/赞成显示/隐藏分离方法的注释。这是因为,在我的第二个选项卡我得到一个异常的第二次我输入(当然了第二个选项卡片段的糟糕的设计)。在这里,异常:

Please note the comment on attach/detach method in favor of show/hide. This is because in my SECOND tab I get an exception the 2nd time I enter it (surely for bad design of second Tab Fragment). Here the exception:

10-18 11:10:13.093: E/AndroidRuntime(3777): Caused by: java.lang.IllegalArgumentException: Binary XML file line #38: Duplicate id 0xffffffff, tag sistemiList, or parent id 0x0 with another fragment for it.cpmapave.mt.ui.intervento.SistemaAlimentazioneListFragment_

(删除标记和的id &LT;片段机器人:名称=it.cpmapave.mt.ui.intervento.SistemaAlimentazioneListFragment_/&GT; 解决问题..但我需要一种方式来获得它以刷新自己的适配器!所以,我显示/隐藏片段,而不是连接/断开)。幸运的是,我必须解决的方向,所以我有重叠的碎片使用显示/隐藏,当方向改变没有问题的。

(Removing the tag and the id of <fragment android:name="it.cpmapave.mt.ui.intervento.SistemaAlimentazioneListFragment_" /> solve the problem.. but I need a way to get it in order to refresh his adapter! So I show/hide Fragments instead of attach/detach). Luckily I have to fix orientation so I have no problem of overlapping fragments when orientation changes using show/hide.

下面是第一个选项卡显示DatiFragment,它扩展片段类。

Here is the FIRST tab display DatiFragment, which extends Fragment class.

这似乎是确定!

这里是第二个选项卡,其中显示StrumentiFragment上。这里是布局:

And here is on the SECOND tab, which displays StrumentiFragment. Here is the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
       android:layout_height="fill_parent"
       android:layout_width="wrap_content"
       android:gravity="center_vertical"
       android:text="Sistema di alimentazione:" />

    <Spinner 
        android:id="@+id/sistemiDiAlimentazione"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content" />

     <Button 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Aggiungi"
        android:id="@+id/addSistemaAlimentazione" />           

</LinearLayout>
<LinearLayout
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <fragment android:name="it.cpmapave.mt.ui.intervento.SistemaAlimentazioneListFragment_"
        android:layout_width="0dp"
        android:tag="sistemiList"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <FrameLayout android:id="@+id/item_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />
</LinearLayout>

添加从微调的片段A的一个元素更新FragmentList B.选择从C片段片段列表B更新细节的元素。

Adding an element from Spinner in Fragment A update the FragmentList B. Selecting an element from Fragment List B update details in the Fragment C.

我认为这个问题可能是由嵌套的碎片造成的..但我不知道该怎么CON我设计它以不同的方式..也许我做比它确实是它更加困难。

I think the problem could be caused by nested Fragments.. but I don't know how con I design it in a different way.. maybe I make it out to be more difficult than it really is..

我省略说和我使用ActionBarSherlock的code显示,不仅是因为我觉得是不是因为我的问题!

I've omitted to say and to show in the code that I'm using ActionBarSherlock, only because I think that is not cause of my problem!

所以最终......问题是:

So at the end.. the question is:

我需要获得的功能布局像一个显示在第二张照片。但是,我得到它只是通过嵌套片段。我必须避免这样做。我该怎么办?

感谢您的任何答复 马可

Thank you for any response Marco

推荐答案

我已经解决了将每个框架中的FrameLayout,显示/隐藏在需要的时候。

I've solved putting each Frame in a FrameLayout, showing/hiding when needed.

我也定义了,我创造了我的ActionTab类自定义布局:

I've also defined a custom layout for the class where I created my ActionTab:

<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabWidget
        android:id="@android:id/tabs"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <LinearLayout
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+android:id/navtabcontent"
            android:layout_width="0dp"
            android:layout_height="600dp"
            android:layout_weight="1"/>
        <FrameLayout
            android:id="@+android:id/realtabcontent"
            android:layout_width="0dp"
            android:layout_height="600dp"
            android:layout_weight="3"/>
    </LinearLayout>
</LinearLayout>

我希望这能成为有用的人!

I hope this can be useful for someone!