本文介绍了如何添加/ ON按钮删除片段点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在present我有,我现在用的将我的片段RELATIVE_LAYOUT容器。
我使用一个按钮OnClickListener加载XML片段布局到RelativeLayout的容器中。

我想实现的是,当我preSS按钮一次,该片段应加载......当我preSS再次片段应该被删除。我已经使用整数确定是否片段加载已经尝试过,但失败了。
任何帮助鸭preciated ...

code:

 公共类MainActivity延伸活动{
    按钮B1,B2;
    INT boolb1 = 0,boolb2 = 0;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        B1 =(按钮)findViewById(R.id.btn1);
        B2 =(按钮)findViewById(R.id.btn2);        B1.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                FragmentManager FM = getFragmentManager();
                FragmentTransaction英尺= fm.beginTransaction();
                FragmentOne F1 =新FragmentOne();                如果(boolb1 == 0)
                {ft.add(R.id.frg1,F1);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                boolb1 = 1;}
                其他
                {ft.remove(F1);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
                boolb1 = 0;}
                //ft.addToBackStack(\"f1);
                ft.commit();
            }
        });        B2.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                FragmentManager FM = getFragmentManager();
                FragmentTransaction英尺= fm.beginTransaction();
                FragmentTwo F2 =新FragmentTwo();                如果(boolb2 == 0){
                   ft.add(R.id.frg2,F2);
                   ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                   boolb2 = 1;
                }其他{
                   ft.remove(F2);
                   ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
                   boolb2 = 0;
                }                //ft.addToBackStack(\"f2);
                ft.commit();
            }
        });
    }


解决方案

您的问题是,您创建一个新的 片段每次点击按钮。您需要获取到当前添加 片段然后删除一个参考。

此外,您不再需要使用该标志。
当添加片段,你可以的标签它。一旦删除片段,您可以使用用于添加标签片段来获取到当前已添加的引用片段

下面是你应该如何做到这一点的例子:

 私人按钮B1;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    B1 =(按钮)findViewById(R.id.btn1);    B1.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            FragmentManager FM = getFragmentManager();
            FragmentTransaction英尺= fm.beginTransaction();
            FragmentOne F =(FragmentOne)fm.findFragmentByTag(标记);            如果(F == NULL)未加入{//
                F =新FragmentOne();
                ft.add(R.id.frg1,F标记);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);            }其他{//已经添加                ft.remove(F);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
            }            ft.commit();
        }
    });    // 等等 ...
}

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.

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. You need to get a reference to the currently added Fragment and then remove that one.

Furthermore, you no longer need to use the flag.When adding the Fragment, you can tag it. Upon removing the Fragment, you can use the tag used for adding the Fragment to get a reference to the currently added 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 ...
}

这篇关于如何添加/ ON按钮删除片段点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:55