简体   繁体   English

删除linearlayout内部的2个片段

[英]Remove 2 fragment inside linearlayout

I am new to Android programming. 我是Android编程的新手。 I have tried to make 2 fragment show in leave_content_container 我试图在leave_content_container中显示2个片段

      Fragment fragment = getFragmentManager().findFragmentById(R.id.leave_content_container);
    getFragmentManager().beginTransaction().remove(fragment).commit();
    getFragmentManager().beginTransaction()
            .add(new ExpandableDataProviderFragmentLeave(), FRAGMENT_TAG_EXPANDABLE_DATA_PROVIDER_LEAVE)
            .commit();
    getFragmentManager().beginTransaction()
            .add(R.id.leave_content_container, new EmployeeLeaveAddExpandableFragment(), FRAGMENT_LIST_VIEW)
            .commit();
    getFragmentManager().beginTransaction()
            .add(new ExpandableDataProviderFragmentLeave1(), FRAGMENT_TAG_EXPANDABLE_DATA_PROVIDER_LEAVE_1)
            .commit();
    getFragmentManager().beginTransaction()
            .add(R.id.leave_content_container, new EmployeeLeaveAddExpandableFragment1(), FRAGMENT_LIST_VIEW)
            .commit();

When I am done I want to remove all this fragment and replace it with new another fragment 完成后,我想删除所有这些片段,并用新的另一个片段替换它

  Fragment fragment = getFragmentManager().findFragmentById(R.id.leave_content_container);
            getFragmentManager().beginTransaction().remove(fragment).commit();
            getFragmentManager().beginTransaction().add(new ExpandableDraggableSwipeableDataProviderFragmentLeave(), FRAGMENT_TAG_DATA_PROVIDER).commit();
            getFragmentManager().beginTransaction()
                    .add(R.id.leave_content_container, new ExpandableDraggableSwipeableFragmentLeave(), FRAGMENT_LIST_VIEW)
                    .commit();

The problem is when I remove two of the older fragment only one is removed. 问题是,当我删除两个较旧的片段时,只有一个被删除。 How do I remove all of the fragment inside leave_content_container? 如何删除leaves_content_container中的所有片段? Sorry for my bad English. 对不起,我的英语不好。

You could easily remove all of fragments by doing this : 您可以通过执行以下操作轻松删除所有片段:

getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.leave_content_container)).commit();

It will remove the fragments that is loded in your container. 它将删除容器中的碎片。

You can delete the fragment you want by its tag: 您可以通过其标签删除所需的片段:

    Fragment frag1 = getFragmentManager().findFragmentByTag(FRAGMENT_TAG_1);
    Fragment frag2 = getFragmentManager().findFragmentByTag(FRAGMENT_TAG_2);

    //P.S: multiple operations can be done in the same transaction
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction
            .remove(frag1)
            .remove(frag2)
            .commit();

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

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