2014-04-25 92 views
2

嗨,我很新的Android。我尝试替换布局文件中静态添加的片段。但是,如果我在我的xml布局中实例化了我的Fragment,那么在运行时用另一个Fragment替换它之后内容将保持可见。Android片段复制:无法替代静态添加的片段

这是我的活动代码:

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代码:

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

}

在此先感谢。

回答

2

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

(如果你想有示出的默认片段的活动开始时,在活动的onCreate方法复制FragmentTransaction代码,并用片段交换空白的FrameLayout视图)