Gabriel Gomes Gabriel Gomes -4 years ago 347
Android Question

Fragments Xamarin Android

Hy Everybody,

I trying to replace the fragment but its dont work... I have a Activity, inside this Activity i have a two Buttons and a LinearLayout, and i want to replace many fragments in that LinearLayout using button Next and Previuos, the first fragment work, but the second doesnt work.

Can someone explain me how to do this?

Activity

ing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V7.App;
using Uer.Fragments.FragmentsRegister;

namespace Uer
{
[Activity(Label = "Register", Theme = "@style/NoActionBar")]

public class RegisterActivity : AppCompatActivity
{

private LinearLayout lnlContainerRegister;
private Button btnNext;

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Create your application here
SetContentView(Resource.Layout.RegisterActivity);

lnlContainerRegister = FindViewById<LinearLayout>(Resource.Id.lnlContainerRegister);
btnNext = FindViewById<Button>(Resource.Id.btnNext);

cpfFragment();

btnNext.Click += BtnNext_Click;
}

private void BtnNext_Click(object sender, EventArgs e)
{
emailFragment();
}

void cpfFragment()
{

var transaction = SupportFragmentManager.BeginTransaction();
transaction.Add(Resource.Id.lnlContainerRegister, new CPF(), "CPF");
transaction.Commit();

}

void emailFragment()
{
var transaction = SupportFragmentManager.BeginTransaction();
transaction.Add(Resource.Id.lnlContainerRegister, new Email(), "Email");
transaction.Commit();
}
}
}


CPF Fragment

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;

namespace Uer.Fragments.FragmentsRegister
{
public class CPF : 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
View view = inflater.Inflate(Resource.Layout.CPFFragment, container, false);

return view;
}
}
}


Email Fragment

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;

namespace Uer.Fragments.FragmentsRegister
{
public class Email : 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
View view = inflater.Inflate(Resource.Layout.EmailFragment, container, false);

return view;
}
}
}


PS: The cpfFragment works normaly!

Answer Source

Try to add a FrameLayout to your Activity's XML.

<FrameLayout
        android:id="@+id/myFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

Then, Use the method Replace() of your supportFragmentManager, instead of the method Add().

You'll get something like this :

void cpfFragment()
        {

            var transaction = SupportFragmentManager.BeginTransaction();
            transaction.Replace(Resource.Id.myFrameLayout, new CPF(), "CPF");
            transaction.Commit(); //or CommitAllowingStateLoss

        }

With this code, the FrameLayout of your activity will contain the view of the fragment CPF.

Then, you will have to call the same code for your Email fragment, and the FrameLayout will be cleaned up and the view of the Email fragment will be added into it.

    void emailFragment()
    {
        var transaction = SupportFragmentManager.BeginTransaction();
        transaction.Replace(Resource.Id.myFrameLayout, new Email(), "Email");
        transaction.Commit(); //or CommitAllowingStateLoss
    }

Tell me if it works :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download