简体   繁体   English

如何使用基于标记的另一个片段替换片段管理器中的现有片段

[英]how to replace existing fragment in a fragment manager with another fragment based on tag

i have an activity that will show data using fragment, for each fragment i give the unique id as the Tag like this: 我有一个活动,将使用片段显示数据,对于每个片段,我给出唯一的ID作为标签,如下所示:

public class MyActivity .... {

    void addFragment(String id){
        // this is how i add a new fragment
        FragmentTransaction fTrans = getFragmentManager().beginTransaction();

        AnyFragment frag = new AnyFragment();

        fTrans.add(R.id.container, frag, id);
        fTrans.commit();
    }

    void replaceFragment(String id){
        // this is how i replace the fragment with tag 'id' with new fragment
        FragmentTransaction fTrans = getFragmentManager().beginTransaction();

        AnyFragment newFrag = new AnyFragment();
        fTrans.replace(R.id.container, newFrag, id);
        fTrans.commit();
    }

}

In my example i have add 2 fragment with tag ' data1 ' and ' data2 '. 在我的例子中,我添加了2个带有标签' data1 '和' data2 '的片段。

When i'm tried to replace the fragment with tag ' data1 ' or ' data2 ', it always replacing the fragment which added first and when i'm replace for the second time, it replace the second added fragment. 当我试图用标签' data1 '或' data2 '替换片段时,它总是替换添加的片段,当我第二次替换时,它会替换第二个添加的片段。

When i'm replacing for 3 times, it leave me only one fragment and one other fragment is gone. 当我更换3次时,它只留下一个片段而另一个片段消失了。

My problem is: 我的问题是:

I want to replace the fragment with specified tag with a new fragment, how to do that? 我想用一个新的片段替换带有指定标签的片段,怎么做? There's something wrong for replace in my code? 在我的代码中替换有什么问题?

Can i use replace method? 我可以使用替换方法吗? or i must use remove the fragment with specified tag first then add the new fragment with same tag? 或者我必须首先使用指定标签删除片段然后添加具有相同标签的新片段? What's the best approach to do that? 这样做的最佳方法是什么? It would be great if you can give some explanation, for each approach. 如果你能为每种方法提供一些解释,那就太好了。

Any answer would be appreciated, thanks! 任何答案将不胜感激,谢谢!

You don't really need tags in your case: 在你的情况下,你真的不需要标签:

Check the method description on Android Developer: 检查Android Developer上的方法说明

Replace an existing fragment that was added to a container. 替换添加到容器的现有片段。 This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here. 这与使用相同的containerViewId添加的所有当前添加的片段调用remove(Fragment),然后使用此处给出的相同参数添加(int,Fragment,String)基本相同。

So all you do is: 所以你要做的就是:

  1. During onCreate() of Activity (or whenever you actually need to instantiate a fragment) - fill the container with a new fragment: getSupportFragmentManager().beginTransaction().replace(R.id.container, Fragment.instantiate(this, FragmentA.class.getName())).commit(); 在Activity的onCreate()期间(或者实际需要实例化片段时) - 使用新片段填充容器: getSupportFragmentManager().beginTransaction().replace(R.id.container, Fragment.instantiate(this, FragmentA.class.getName())).commit();

  2. Once you need to replace FragmentA with FragmentB, you just call getSupportFragmentManager().beginTransaction().replace(R.id.container, Fragment.instantiate(this, FragmentB.class.getName())).commit(); 一旦你需要用FragmentB替换FragmentA,你只需要调用getSupportFragmentManager().beginTransaction().replace(R.id.container, Fragment.instantiate(this, FragmentB.class.getName())).commit(); (ie to tags required - R.id.container has only one fragment at the same time) (即需要标签 - R.id.container同时只有一个片段)

If you don't want to replace fragments (which is preferred solution), you can in onCreate() of your activity add() both fragments, and then use show() and hide() methods respectively: 如果您不想replace片段(这是首选解决方案),您可以在活动的onCreate()add()两个片段,然后分别使用show()hide()方法:

    Fragment fragmentA = Fragment.instantiate(this, FragmentA.class.getName());
    Fragment fragmentB = Fragment.instantiate(this, FragmentA.class.getName());
    ..
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, fragmentA)
            .add(R.id.container, fragmentB)
            .show(fragmentA)
            .hide(fragmentB)
            .commit();

    ..
    // Once you want to show fragment B instead of fragment A
    getSupportFragmentManager().beginTransaction()
            .show(fragmentB)
            .hide(fragmentA)
            .commit();

UPD: regarding editing of the existing fragment: UPD:关于现有片段的编辑:

In your Activity, once something happened and you want to update the fragment - do this: 在您的Activity中,一旦发生某些事情并且您想要更新片段 - 请执行以下操作:

  1. In onCreate() (or any other suitable place) you add fragment into container( R.id.container ): getSupportFragmentManager().beginTransaction().replace(R.id.container, Fragment.instantiate(this, FragmentA.class.getName())).commit(); onCreate() (或任何其他合适的地方),你将片段添加到容器( R.id.container ): getSupportFragmentManager().beginTransaction().replace(R.id.container, Fragment.instantiate(this, FragmentA.class.getName())).commit();

  2. In the place where you want to update - do this: 在您要更新的位置 - 执行以下操作:

     Fragment f = getSupportFragmentManager().findFragmentById(R.id.container); if (f instanceof FragmentA) { // doSomething() is a public method of FragmentA; ((FragmentA)f).doSomething(); } 

I hope, it helps 我希望,这有帮助

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

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