Android - Start Fragemnt From Another Fragment C#
I know this question has been asked too much here but i ahve tried every solution out there to open a fragment from another fragment and no one worked for me. Fragment1.cs public o
Solution 1:
You need a FrameLayout
to contain your LinearLayout
:
<?xml version="1.0" encoding="utf-8"?><FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/content_frame"><LinearLayoutandroid:id="@+id/ll"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:text="Text"android:layout_width="match_parent"android:layout_height="70dp"android:id="@+id/total"android:textAlignment="center"android:textSize="30dp" /><TextViewandroid:text="Text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/textchangevalue"android:textSize="33dip"android:textStyle="bold" /><Buttonandroid:text="UPDATE"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/updatebtn" /><Buttonandroid:text="ADD"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/addbtn" /><ListViewandroid:minWidth="25px"android:minHeight="25px"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/portfoliolist"android:clickable="true" /></LinearLayout></FrameLayout>
And your code should be :
publicoverride View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragmentvar view = inflater.Inflate(Resource.Layout.my_layout, container, false);
var ll = view.FindViewById<LinearLayout>(Resource.Id.ll);
var bt = view.FindViewById(Resource.Id.addbtn);
bt.Click+= delegate
{
Fragment2 fragment2 = new Fragment2();
FragmentTransaction ft = FragmentManager.BeginTransaction();
ft.Replace(Resource.Id.content_frame, fragment2);
ft.Commit();
ll.Visibility = ViewStates.Gone;
};
return view;
}
You can refer to this to see why your fragment2
just top on it. I use ll.Visibility = ViewStates.Gone;
to avoid it.
Solution 2:
You have to attach your Click
event to your button beforereturn
the view
, and I think that you want to attach this event on your addbtn
so you have to create the variable add
before to attach this event
publicoverride View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
View view = inflater.Inflate(Resource.Layout.my_layout, container, false);
Button add = view.FindViewById<Button>(Resource.Id.addbtn);
add.Click += delegate
{
Fragment2 fragment2 = new Fragment2();
FragmentTransaction ft = FragmentManager.BeginTransaction();
ft.Replace(Resource.Id.content_frame, fragment2);
ft.Commit();
};
return view;
}
Solution 3:
You can use the events instead:
publicoverride View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
View view = inflater.Inflate(Resource.Layout.my_layout, container, false);
Button add = view.FindViewById<Button>(Resource.Id.addbtn);
add.Click += (object sender, EventArgs e) =>
{
Fragment2 fragment2 = new Fragment2();
FragmentTransaction ft = FragmentManager.BeginTransaction();
ft.Replace(Resource.Id.content_frame, fragment2);
ft.Commit();
};
return view;
}
Post a Comment for "Android - Start Fragemnt From Another Fragment C#"