替换子片段
本文关键字:片段 替换 | 更新日期: 2023-09-27 18:14:18
我是Android开发新手。我使用Xamarin Android开发一个Android应用程序。我有问题时,在替换碎片碎片,在一个模板应用程序上工作,我想用FragmentB代替FragmentA。在Main Activity中,content_frame被FragmentA取代。
这是我目前拥有的代码
主要。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解决方案改变我的代码。
提前感谢所有或任何帮助。
要用片段B替换片段A,你必须做的与添加片段A时所做的几乎相同:
Fragment fragB = new FragmentB();
FragmentManager.BeginTransaction().Replace(Resource.Id.parent_fragment, fragB).Commit();
只要确保你把片段放在主容器上(在你的主布局中的那个)