Android片段重复:无法替换静态添加的片段

时间:2014-04-25 11:24:17

标签: android-fragments

嗨,我是Android新手。我尝试替换布局文件中静态添加的片段。但是,如果我在我的xml布局中实例化我的Fragment,那么在运行时将其替换为另一个Fragment后,内容将保持可见。

这是我的活动代码:

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;

public class RetailerActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_retailer);
}

public void selectFrag(View view) {
    Fragment fr;

    if (view == findViewById(R.id.button2)) {
        fr = new FragmentTwo();
    } else if (view == findViewById(R.id.button3)) {
        fr = new FragmentThree();
    } else {
        fr = new FragmentOne();
    }

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fr);
    fragmentTransaction.commit();

 }
}

这是我的活动XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="selectFrag"
    android:text="Fragment No.1" />

<Button
    android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="selectFrag"
    android:text="Fragment No.2" />

<Button
    android:id="@+id/button3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="selectFrag"
    android:text="Fragment No.3" />

<LinearLayout
android:orientation="vertical"
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<fragment
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" />

</LinearLayout>

FragmentOne XML文件:

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:orientation="vertical"
   android:background="#00ffff">

   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:text="This is fragment No.1"
       android:textStyle="bold" />

  </LinearLayout>

FragmentOne java文件:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_one, container, false);
}

}

FragmentTwo xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" 
   android:background="#00ffff">
   <TextView
       android:id="@+id/textfrag2"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="This is fragment No.2"
       android:textStyle="bold" />

</LinearLayout>

FragmentTwo java code:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTwo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_two, container, false);
}

}

提前致谢。

1 个答案:

答案 0 :(得分:2)

我也研究过这个问题,我似乎无法找到将片段静态放置在XML文件中的解决方案。在您的Activity XML文件中,您必须使用FrameLayout替换Fragment部分(id可以保持不变,只需将'Fragment'替换为'FrameLayout');使用FragmentTransaction制作selectFrag方法的唯一方法是替换片段而不重叠。

(如果您希望在活动开始时显示默认片段,请复制Activity的onCreate方法中的FragmentTransaction代码,并将空白FrameLayout视图与任一片段交换)