Replace 在Xamarin.Android中,如何在点击按钮时用另一个片段替换一个片段?

Replace 在Xamarin.Android中,如何在点击按钮时用另一个片段替换一个片段?,replace,xamarin.android,fragment,buttonclick,Replace,Xamarin.android,Fragment,Buttonclick,我希望有以下屏幕: 当我单击图像1和图像2按钮时,我希望在图像区域中分别显示Image1.jpg和Image2.jpg。我希望Image1.jpg和Image2.jpg处于不同的片段中,并在单击按钮时替换这些片段,而不是简单地在单击按钮时更改图像区域中ImageView的android:src。我希望Image1.jpg成为打开活动时出现的第一个图像。我在Xamarin.Android中找不到如何使用片段来实现这一点,您可以先创建两个片段及其xml 片段一: 及其xml: 及其xml: 然后在M

我希望有以下屏幕:

当我单击图像1和图像2按钮时,我希望在图像区域中分别显示Image1.jpg和Image2.jpg。我希望Image1.jpg和Image2.jpg处于不同的片段中,并在单击按钮时替换这些片段,而不是简单地在单击按钮时更改图像区域中ImageView的android:src。我希望Image1.jpg成为打开活动时出现的第一个图像。我在Xamarin.Android中找不到如何使用片段来实现这一点,您可以先创建两个片段及其xml

片段一:

及其xml:

及其xml:

然后在MainActivity中,您可以按如下方式设置其xml:

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

    <LinearLayout
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="80dip"
        android:orientation="horizontal">

        <Button
            android:id="@+id/buttonone"
            android:layout_width="150dip"
            android:layout_height="80dip"
            android:text="FragmentOne"/>

        <Button
            android:id="@+id/buttontwo"
            android:layout_width="150dip"
            android:layout_height="80dip"
            android:text="FragmentTwo"/>

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
        android:layout_height="430dip">

    </RelativeLayout> 

</LinearLayout>
最后,在MainActivity.cs中实现此功能:

public class MainActivity : AppCompatActivity
{
    Android.Support.V4.App.Fragment fragmentOne;
    Android.Support.V4.App.Fragment fragmentTwo;
    Android.Support.V4.App.FragmentTransaction fragmentManager;
    [Obsolete]
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        Button buttonone = FindViewById<Button>(Resource.Id.buttonone);
        buttonone.Click += Buttonone_Click;

        Button buttontwo = FindViewById<Button>(Resource.Id.buttontwo);
        buttontwo.Click += Buttontwo_Click;
        fragmentOne = new Fragment1();
        fragmentTwo = new Fragment2();

        fragmentManager = SupportFragmentManager.BeginTransaction();
        fragmentManager.Add(Resource.Id.containerView, fragmentOne);
        //adding first fragment when entering activity
        fragmentManager.Commit();
    }

    private void Buttonone_Click(object sender, System.EventArgs e)
    {
        //throw new System.NotImplementedException();
        Console.WriteLine("Buttonone_Click");
        fragmentManager = SupportFragmentManager.BeginTransaction();
        // replace to be the first fragment 
        fragmentManager.Replace(Resource.Id.containerView, fragmentOne);
        fragmentManager.Commit();
    }

    private void Buttontwo_Click(object sender, System.EventArgs e)
    {
        //throw new System.NotImplementedException();
        Console.WriteLine("Buttontwo_Click");

        fragmentManager = SupportFragmentManager.BeginTransaction();
        // replace ro be the second fragment
        fragmentManager.Replace(Resource.Id.containerView, fragmentTwo);
        fragmentManager.Commit();
    }
}
效果如下:

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

    <LinearLayout
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="80dip"
        android:orientation="horizontal">

        <Button
            android:id="@+id/buttonone"
            android:layout_width="150dip"
            android:layout_height="80dip"
            android:text="FragmentOne"/>

        <Button
            android:id="@+id/buttontwo"
            android:layout_width="150dip"
            android:layout_height="80dip"
            android:text="FragmentTwo"/>

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
        android:layout_height="430dip">

    </RelativeLayout> 

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment2">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/th2"/>

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

    <LinearLayout
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="80dip"
        android:orientation="horizontal">

        <Button
            android:id="@+id/buttonone"
            android:layout_width="150dip"
            android:layout_height="80dip"
            android:text="FragmentOne"/>

        <Button
            android:id="@+id/buttontwo"
            android:layout_width="150dip"
            android:layout_height="80dip"
            android:text="FragmentTwo"/>

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
        android:layout_height="430dip">

    </RelativeLayout> 

</LinearLayout>
public class MainActivity : AppCompatActivity
{
    Android.Support.V4.App.Fragment fragmentOne;
    Android.Support.V4.App.Fragment fragmentTwo;
    Android.Support.V4.App.FragmentTransaction fragmentManager;
    [Obsolete]
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        Button buttonone = FindViewById<Button>(Resource.Id.buttonone);
        buttonone.Click += Buttonone_Click;

        Button buttontwo = FindViewById<Button>(Resource.Id.buttontwo);
        buttontwo.Click += Buttontwo_Click;
        fragmentOne = new Fragment1();
        fragmentTwo = new Fragment2();

        fragmentManager = SupportFragmentManager.BeginTransaction();
        fragmentManager.Add(Resource.Id.containerView, fragmentOne);
        //adding first fragment when entering activity
        fragmentManager.Commit();
    }

    private void Buttonone_Click(object sender, System.EventArgs e)
    {
        //throw new System.NotImplementedException();
        Console.WriteLine("Buttonone_Click");
        fragmentManager = SupportFragmentManager.BeginTransaction();
        // replace to be the first fragment 
        fragmentManager.Replace(Resource.Id.containerView, fragmentOne);
        fragmentManager.Commit();
    }

    private void Buttontwo_Click(object sender, System.EventArgs e)
    {
        //throw new System.NotImplementedException();
        Console.WriteLine("Buttontwo_Click");

        fragmentManager = SupportFragmentManager.BeginTransaction();
        // replace ro be the second fragment
        fragmentManager.Replace(Resource.Id.containerView, fragmentTwo);
        fragmentManager.Commit();
    }
}