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

问题描述 投票:0回答:1

我想要以下屏幕:

enter image description here

当我单击IMAGE 1IMAGE 2按钮时,我希望在IMAGE ZONE中分别显示Image1.jpgImage2.jpg。我希望Image1.jpgImage2.jpg位于不同的片段中,并在单击按钮时替换这些片段,而不是仅在单击按钮时更改android:srcImageViewIMAGE ZONE。我希望Image1.jpg是打开活动时出现的第一张图像。我在Xamarin.Android

中找不到如何处理片段的方法
replace xamarin.android fragment buttonclick
1个回答
0
投票

您可以先创建两个Fragment及其xml

FragmentOne

public class Fragment1 : Android.Support.V4.App.Fragment
{
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        return inflater.Inflate(Resource.Layout.layoutFragment1, container, false);

        //return base.OnCreateView(inflater, container, savedInstanceState);
    }
}

及其xml

<?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/fragment1">

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

</LinearLayout>

FragementTwo

public class Fragment2 : Android.Support.V4.App.Fragment
{
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        return inflater.Inflate(Resource.Layout.layoutFragment2, container, false);

        //return base.OnCreateView(inflater, container, savedInstanceState);
    }
}

及其xml

<?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>

然后在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 = FragmentManager.BeginTransaction();
        fragmentManager = SupportFragmentManager.BeginTransaction();
        fragmentManager.Add(Resource.Id.containerView, fragmentOne);
        fragmentManager.Add(Resource.Id.containerView, fragmentTwo);
        fragmentManager.Commit();
    }

    private void Buttonone_Click(object sender, System.EventArgs e)
    {
        //throw new System.NotImplementedException();
        Console.WriteLine("Buttonone_Click");
        fragmentManager = SupportFragmentManager.BeginTransaction();
        fragmentManager.Hide(fragmentTwo);
        fragmentManager.Show(fragmentOne);
        fragmentManager.Commit();

        //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();
        fragmentManager.Hide(fragmentOne);
        fragmentManager.Show(fragmentTwo);
        fragmentManager.Commit();

        //fragmentManager.Replace(Resource.Id.containerView, fragmentTwo);
        //fragmentManager.Commit();
    }
}

效果如下:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.