片段删除问题 [英] fragment remove issue

查看:138
本文介绍了片段删除问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚学段今天。我preSS一个按钮,并将其添加/删除片段。但是,如果我尝试从一个我想删除被删除除删除片段的每个片段,为什么呢?第一preSS正确添加一个片段。

  Button2的片段:

 Button按钮=(按钮)view.findViewById(R.id.button2);
        button.setOnClickListener(新View.OnClickListener(){
          @覆盖
          公共无效的onClick(视图v){

              ButtonFragment片段=新ButtonFragment();
              如果(片段=空&安培;!&安培; fragment.isVisible()){



                  FragmentManager fragmentManager = getFragmentManager();

                  FragmentTransaction交易= fragmentManager.beginTransaction();
                  transaction.remove(片段).commit();

              }
              否则如果(!fragment.isVisible())
              {
                  FragmentManager fragmentManager = getFragmentManager();

                  FragmentTransaction交易= fragmentManager.beginTransaction();
                  transaction.add(R.id.fragment_container,片段).commit();
  }

          }
        });
        返回查看;
      }
}
 

我有两个片段像这样的XML: 当我点击按钮欲被添加以xml没有定义的片段,它是。 但下一次我preSS的按钮,这应该删除片段。一切都是从那个片段除了删除。

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=横向
    机器人:后台=#123456
    机器人:ID =@ + ID / fragment_container>


    <片段
        机器人:ID =@ + ID / TimeFragment
        机器人:layout_width =0dp
        机器人:layout_weight =2
        机器人:layout_height =match_parent
        类=com.example.myfragment.TimeFragment>
        !<  -  preVIEW:布局= @布局/详细信息 - >
    &所述; /片断>



    <片段
        机器人:ID =@ + ID / Button2Fragment
        机器人:layout_width =0dp
        机器人:layout_weight =3
        机器人:layout_height =match_parent
        类=com.example.myfragment.Button2Fragment>
        !<  -  preVIEW:布局= @布局/详细信息 - >
    &所述; /片断>

< / LinearLayout中>
 

解决方案

您不能删除 Framgnet 您在使用XML增加了。如果要删除通过卸下摆臂法的片段,你应该首先它通过添加到您的布局。新增方法,而不是将其嵌入到XML文件中。在这种情况下,你只能 .show .hide 片段

更新:

要添加 ButtonFragment 动态做到这一点:

  ButtonFragment buttonsFragment =新ButtonFragment();
newfragmentTransaction = fragmentManager.beginTransaction();
newfragmentTransaction.add(R.id.containerForFragments,buttonsFragment).commit();
 

更新2: 这code:

 按钮按钮=(按钮)view.findViewById(R.id.button2);
    button.setOnClickListener(新View.OnClickListener(){
      @覆盖
      公共无效的onClick(视图v){

          ButtonFragment片段=新ButtonFragment();
          如果(片段=空&安培;!&安培; fragment.isVisible()){



              FragmentManager fragmentManager = getFragmentManager();

              FragmentTransaction交易= fragmentManager.beginTransaction();
              transaction.remove(fragmentManager.findFragmentById(R.layout.activity_main))提交()。

          }
          否则如果(!fragment.isVisible())
          {
              FragmentManager fragmentManager = getFragmentManager();

              FragmentTransaction交易= fragmentManager.beginTransaction();
              transaction.add(R.layout.activity_main,片段).commit();
          }

      }
    });
 

应该从活动执行,而不是从片段

I am just learning fragments today. I press a button and it adds/removes a fragment. However if I try remove the fragment every fragment apart from the one I want removed is removed, why? The first press correctly adds a fragment.

Button2 fragment:

 Button button = (Button) view.findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {

              ButtonFragment fragment = new ButtonFragment();
              if (fragment != null && fragment.isVisible()) {



                  FragmentManager fragmentManager = getFragmentManager();

                  FragmentTransaction transaction =  fragmentManager.beginTransaction(); 
                  transaction.remove(fragment).commit();

              }
              else if(!fragment.isVisible())
              {
                  FragmentManager fragmentManager = getFragmentManager();

                  FragmentTransaction transaction =  fragmentManager.beginTransaction(); 
                  transaction.add(R.id.fragment_container, fragment ).commit();
  }       

          }
        });
        return view;
      }
}

I have two fragments like this in xml: When I click the button I want the fragment not defined in xml to be added, and it is. However the next time i press the button, which should remove that fragment. Everything is removed apart from that fragment.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="#123456"
    android:id="@+id/fragment_container"  >


    <fragment
        android:id="@+id/TimeFragment"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"
        class="com.example.myfragment.TimeFragment" >
        <!-- Preview: layout=@layout/details -->
    </fragment>



    <fragment
        android:id="@+id/Button2Fragment"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="match_parent"
        class="com.example.myfragment.Button2Fragment" >
        <!-- Preview: layout=@layout/details -->
    </fragment>

</LinearLayout> 

解决方案

You cant remove a Framgnet that you have added using the XML. If you want to remove the fragment via the .remove method you should first add it to your layout via the .add method, and not embed it into the XML file. in this case you can only .show or .hide the Fragments.

UPDATE:

To add the ButtonFragment dynamically do this:

ButtonFragment buttonsFragment = new ButtonFragment();
newfragmentTransaction = fragmentManager.beginTransaction();
newfragmentTransaction.add(R.id.containerForFragments, buttonsFragment ).commit();

UPDATE 2: This code:

  Button button = (Button) view.findViewById(R.id.button2);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

          ButtonFragment fragment = new ButtonFragment();
          if (fragment != null && fragment.isVisible()) {



              FragmentManager fragmentManager = getFragmentManager();

              FragmentTransaction transaction =  fragmentManager.beginTransaction(); 
              transaction.remove(fragmentManager.findFragmentById(R.layout.activity_main)).commit();

          }
          else if(!fragment.isVisible())
          {
              FragmentManager fragmentManager = getFragmentManager();

              FragmentTransaction transaction =  fragmentManager.beginTransaction(); 
              transaction.add(R.layout.activity_main, fragment ).commit();
          }       

      }
    });

should be run from the Activity and not from the Fragment.

这篇关于片段删除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆