简体   繁体   English

如何在按钮上添加/删除片段?

[英]How to add/remove Fragment on Button click?

At present I have got a "RELATIVE_LAYOUT" container which I am using for adding my fragment. 目前我有一个“RELATIVE_LAYOUT”容器,我用它来添加我的片段。 I am using OnClickListener on a button to load the fragment XML layout into the RelativeLayout container. 我在按钮上使用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();
            }
        });
    }

Your issue is that you create a new Fragment everytime you click the Button . 您的问题是每次单击Button都会创建一个新的 Fragment You need to get a reference to the currently added Fragment and then remove that one. 您需要获取对当前添加的 Fragment 的引用 ,然后删除该Fragment

Furthermore, you no longer need to use the flag. 此外,您不再需要使用该标志。 When adding the Fragment , you can tag it. 添加Fragment ,您可以标记它。 Upon removing the Fragment , you can use the tag used for adding the Fragment to get a reference to the currently added 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 ...
}

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

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