【问题标题】:fragment remove issue片段删除问题
【发布时间】:2013-03-12 11:32:01
【问题描述】:

我今天只是在学习片段。我按下一个按钮,它会添加/删除一个片段。但是,如果我尝试删除片段,除了我想要删除的片段之外的每个片段都被删除,为什么?第一次按下正确添加了一个片段。

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;
      }
}

我在 xml 中有两个这样的片段: 当我单击按钮时,我希望添加未在 xml 中定义的片段,它就是。 但是,下次我按下按钮时,应该会删除该片段。除了那个片段之外的所有东西都被删除了。

<?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> 

【问题讨论】:

  • 请发布堆栈跟踪 - 这样更容易找到问题
  • 现在添加了,谢谢。似乎现在的痕迹不同了,所以我必须改变一些东西。

标签: java android fragment


【解决方案1】:

您无法删除使用 XML 添加的Framgnet。如果您想通过.remove 方法删除片段,您应该首先通过.add 方法将其添加到您的布局中,而不是将其嵌入到XML 文件中。在这种情况下,您只能.show.hide Fragments

更新:

要动态添加ButtonFragment,请执行以下操作:

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

更新 2: 这段代码:

  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();
          }       

      }
    });

应该从Activity 而不是Fragment 运行。

【讨论】:

  • 如果我不在xml中添加它,我如何完成同样的事情?因为我不能用ButtonFragment fragment = (ButtonFragment) getFragmentManager() .findFragmentById(R.id.ButtonFragment); boolean a = true; if (fragment != null &amp;&amp; fragment.isVisible()) {
  • 谢谢,但这里有什么 R.id.containerForFragments 我不能说 R.id.ButtonFragment 因为它已经从 xml 中删除了
  • 而不是 R.id.containerForFragments,将布局 id 放在要放置 Fragment 的位置。
  • 我试过 R.layout.activity_main 但没用,我试试其他方法谢谢
  • 你把我的编辑读到最后了吗?我写信给你,切换fragment的代码应该从Activity运行,而不是从fragment运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多