Java 如何在按钮单击时添加/删除片段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24517494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:42:00  来源:igfitidea点击:

How to add/remove Fragment on Button click?

javaandroidandroid-fragmentsfragmenttransactionfragmentmanager

提问by Abhi9

At present I have got a "RELATIVE_LAYOUT" container which I am using for adding my fragment. I am using OnClickListener on a button to load the fragment XML layout into the RelativeLayout container.

目前我有一个“RELATIVE_LAYOUT”容器,我用它来添加我的片段。我在按钮上使用 OnClickListener 将片段 XML 布局加载到 RelativeLayout 容器中。

What I want to achieve is that when I press the button once, the fragment should load...and when I press it again the fragment should be removed. I've already tried using integer to identify if fragment is loaded, but failed. Any help Appreciated...

我想要实现的是,当我按下按钮一次时,片段应该加载......当我再次按下它时,片段应该被删除。我已经尝试使用整数来确定片段是否已加载,但失败了。任何帮助表示赞赏...

CODE:

代码:

public class MainActivity extends Activity {
    Button B1,B2;
    int boolb1=0, boolb2=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        B1 = (Button)findViewById(R.id.btn1);
        B2 = (Button)findViewById(R.id.btn2);

        B1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                FragmentOne f1 = new FragmentOne();

                if(boolb1==0)
                {ft.add(R.id.frg1, f1);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                boolb1=1;}
                else
                {ft.remove(f1);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
                boolb1=0;}
                //ft.addToBackStack("f1");
                ft.commit();
            }
        });

        B2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                FragmentTwo f2 = new FragmentTwo();

                if(boolb2==0) {
                   ft.add(R.id.frg2, f2);
                   ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                   boolb2=1;
                } else {
                   ft.remove(f2);
                   ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
                   boolb2=0;
                }

                //ft.addToBackStack("f2");
                ft.commit();
            }
        });
    }

采纳答案by Philipp Jahoda

Your issue is that you create a newFragmenteverytime you click the Button. You need to get a reference to the currently addedFragmentand then remove that one.

你的问题是,你创建一个新的Fragment每次点击的Button。您需要获取对当前添加的引用Fragment,然后删除该引用

Furthermore, you no longer need to use the flag. When adding the Fragment, you can tagit. Upon removing the Fragment, you can use the tag used for adding the Fragmentto get a reference to the currently added Fragment.

此外,您不再需要使用该标志。添加 时Fragment,您可以标记它。删除 后Fragment,您可以使用用于添加 的标记Fragment来获取对当前添加的引用Fragment

Here is an example of how you should do it:

以下是您应该如何操作的示例:

private Button B1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    B1 = (Button)findViewById(R.id.btn1);

    B1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            FragmentOne f = (FragmentOne) fm.findFragmentByTag("tag");

            if(f == null) {  // not added
                f = new FragmentOne();
                ft.add(R.id.frg1, f, "tag");
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

            } else {  // already added

                ft.remove(f);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
            }

            ft.commit();
        }
    });

    // and so on ...
}