Reputation: 139
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!
Upvotes: 0
Views: 2684
Reputation: 1015
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 :)
Edit : to answer the question in comment :
You have to create an Interface, and your Activity should implement it :
public interface IChangeFragment
{
void emailFragment();
void cpfFragment();
}
public class RegisterActivity : AppCompatActivity, IChangeFragment
{
//...
public void emailFragment()
{
//...
}
public void cpfFragment()
{
//...
}
}
Then, your fragments have to use a callback of type IChangeFragment. The callback will be valued in the OnAttach method of your fragment, and you will be able to call methods of your Activity from your Fragment :
public class CPF : Fragment
{
private IChangeFragment m_callBack;
public override void OnAttach(Activity activity)
{
base.OnAttach(activity);
try
{
m_callBack = (IChangeFragment)activity;
}
catch (ClassCastException)
{
throw new ClassCastException(activity.ToString() + " must implement IChangeFragment");
}
}
private void OnButtonClicked(object sender, EventArgs e)
{
m_callBack.emailFragment();
}
}
Note that with this method, any activities using those fragments will have to implement the interface IChangeFragment. If not, you will get an ClassCastException.
Upvotes: 1