我是Android开发的新手。我使用Xamarin Android开发Android应用程序。在替换片段中的片段时遇到问题,在模板应用上工作,我想用FragmentB替换FragmentA。在主要活动中,content_frame将被FragmentA替换。
这是我目前的代码
Main.axml
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="MAIN ACTIVITY" />
<FrameLayout
android:id="@+id/parent_fragment"
android:layout_width="fill_parent"
android:layout_height="200dip" />
</LinearLayout>
MainActivity.cs
[Activity(Label = "NestedFrags", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Fragment fragA = new FragmentA();
FragmentManager.BeginTransaction().Replace(Resource.Id.parent_fragment, fragA).Commit();
}
}
FragmentA.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Parent Fragment"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<FrameLayout
android:id="@+id/child_fragment"
android:layout_width="fill_parent"
android:layout_height="200dip" />
</LinearLayout>
FragmentA.cs
public class FragmentA : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
Fragment fragmentB = new FragmentB();
FragmentTransaction transaction = ChildFragmentManager.BeginTransaction();
transaction.Add(Resource.Id.child_fragment, fragmentB).Commit();
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.fraga, container, false);
return view;
}
}
FragmentB.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Child Fragment of A"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
</LinearLayout>
FragmentB.cs
public class FragmentB : Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.fragb, container, false);
return view;
}
}
到目前为止,我能够将碎片替换为另一个碎片。但我试图用fragmentB完全替换fragmentA。谁能帮帮我吗?我不介意任何人都可以用Java回答,因为我只需要一些工作,我将根据Java解决方案对我的代码进行更改。
提前感谢所有或任何帮助。
答案 0 :(得分:1)
要用片段B替换片段A,你必须做的几乎与你在添加片段A时已经做的一样:
Fragment fragB = new FragmentB();
FragmentManager.BeginTransaction().Replace(Resource.Id.parent_fragment, fragB).Commit();
确保将片段放在主容器(主布局中的那个)上。