FragmentTransaction没有做任何事情

时间:2012-09-14 04:18:30

标签: android fragment fragmenttransaction

我正在学习片段,下面给出的是我的第一个片段程序。一个简单的项目,我有2个屏幕。当我点击第一个屏幕的下一个按钮时,需要显示第二个按钮。

enter image description here

我的目标是Android 2.1及更高版本,使用兼容性套件

AppMainFragmentActivity.java

public class AppMainFragmentActivity extends FragmentActivity {
  @Override
  protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.app_main_layout);
  }
}

app_main_layout.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="vertical" android:id="@+id/fragment_container">

   <fragment class="com.research.fragmentstudy.FirstFragment"     
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
      android:id="@+id/id_first_fragment"/>

</LinearLayout>

FirstFragment.java

public class FirstFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.first_fragment_layout
                 , container,false);

     Button nextButton =(Button) view.findViewById(R.id.button);
     nextButton.setOnClickListener(nextListener);
     return view;
  }

  private OnClickListener nextListener  =   new OnClickListener() {
     @Override
     public void onClick(View v) {
        FragmentManager fm = ((AppMainFragmentActivity)FirstFragment.this
                           .getActivity()).getSupportFragmentManager();
        SecondFragment fragment = new SecondFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_container, fragment);
        ft.commit();
     }
  };
}

first_fragment_layout.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="vertical" android:id="@+id/first_fragment_root">

   <TextView android:layout_height="wrap_content" 
       android:layout_width="fill_parent"
       android:text="Fragment 1" android:gravity="center_horizontal" />

   <Button android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal"
       android:layout_width="wrap_content" android:id="@+id/button"
       android:text="Next" />

</LinearLayout>

SecondFragment.java

public class SecondFragment extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.second_fragment_layout,
                     container,false);
      return view;
   }
}

second_fragment_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:orientation="vertical" android:id="@+id/first_fragment_root">

   <TextView android:layout_height="wrap_content" 
      android:layout_width="fill_parent"
      android:text="Fragment 2" android:gravity="center_horizontal" />

</LinearLayout>

好吧,我得到第一个屏幕好了。现在,

根据我对片段

的理解,我的期望
  • 当我点击屏幕1中的Next按钮时,会创建SecondFragment, 其onCreate()onCreateView()被调用。
  • 显示了SecondFragment,并且FirstFragment被销毁(因为我是 不要将它添加到Backstack)。从那以后就不会有任何动画了 默认片段事务没有动画。

发生了什么

  • SecondFragment正在创建,其onCreate()onCreateView()被调用。
  • 但是FirstFragment仍在屏幕上,第二个从未显示。

现在我对片段的理解可能是错误的。但我相信当我们提交()一个片段事务时,第一个片段应该被第二个片段替换(第一个片段被隐藏或销毁)。好吧似乎没有发生任何事情。这是为什么?我们应该手动销毁/隐藏第一个片段吗?

注意:我知道对于这样一个基本的事情来说这是一个很长的问题。但我把我的整个代码都放了,因为我不确定我把它搞砸了。

3 个答案:

答案 0 :(得分:11)

嗯,我得到了它的工作。这两个问题的答案都正确地说明了我的代码无法按预期工作的一个原因。但是我的代码中有2个错误。

使用fragmentTransaction.add()不会显示旧片段的新片段。你需要调用replace()。答案是正确的。

但我的代码中还有一个错误。您无法替换在xml中静态创建的片段。所以我改变了

app_main_layout.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="vertical" android:id="@+id/fragment_container">

</LinearLayout>

AppMainFragmentActivity.java

public class AppMainFragmentActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.app_main_layout);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FirstFragment fragment = new FirstFragment();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_container,fragment);
        fragmentTransaction.commit();           
    }
}

答案 1 :(得分:6)

您是否尝试过使用ft.replace(R.id.fragment_container,fragment)而不是添加? 添加不会删除现有的片段。

答案 2 :(得分:2)

您正在尝试在fragment_container中添加一个片段,它正在添加,但您的片段不可见,因为您已将id_first_fragment的宽度和高度指定为fill_parent,因此请尝试为id_first_fragment片段设置wrap_content。单击按钮时可以看到下一个片段。如果要关闭第一个片段并显示第二个片段,则需要替换而不是添加。

相关问题