问题描述
我知道这个问题在这里问得太多了,但是我尝试过所有解决方案,从另一个片段中打开一个片段,但没人为我工作.
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 override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
View view = inflater.Inflate(Resource.Layout.my_layout, container, false);
return view;
add.Click += delegate
{
Fragment2 fragment2 = new Fragment2();
FragmentTransaction ft = FragmentManager.BeginTransaction();
ft.Replace(Resource.Id.content_frame, fragment2);
ft.Commit();
};
}
my_layout
<?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/content_frame">
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/total"
android:textAlignment="center"
android:textSize="30dp" />
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textchangevalue"
android:textSize="33dip"
android:textStyle="bold" />
<Button
android:text="UPDATE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/updatebtn" />
<Button
android:text="ADD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addbtn" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/portfoliolist"
android:clickable="true" />
Fragment2.cs
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
View view = inflater.Inflate(Resource.Layout.fragment_lay, container, false);
TextView txt = view.FindViewById<TextView>(Resource.Id.textView1);
Button btn = view.FindViewById<Button>(Resource.Id.button1);
listView = view.FindViewById<ListView>(Resource.Id.listView1);
Toast.MakeText(Activity, "Fragment2 started", ToastLength.Short).Show();
return view;
}
当我运行应用程序并单击add
按钮时,没有任何反应.这是为什么 ?我试图通过放置而不是<LinearLayout>
和<FrameLayout>
来更改Fragment1
的布局,但也无法正常工作.请帮助我找到解决方案.
When I run the app, and click the add
button nothing is happening. Why is that ? I have tried to change the layout of the Fragment1
by putting instead of <LinearLayout>
a <FrameLayout>
but also disn't worked. Please help me to find a solution.
我在Fragment2
中放入了toast
消息,当我单击fragment1
中的add
按钮时,显示toast
消息(在fragment2
中),这表示一切正常,但其布局未显示在屏幕上.
I have put a toast
message in the Fragment2
and when I click the add
button in the fragment1
, the toast
message (which is in the fragment2
) is showing which means that the fragment2
is starting fine but it's layout is not showing on the screen.
推荐答案
您需要FrameLayout
包含您的LinearLayout
:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame">
<LinearLayout
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/total"
android:textAlignment="center"
android:textSize="30dp" />
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textchangevalue"
android:textSize="33dip"
android:textStyle="bold" />
<Button
android:text="UPDATE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/updatebtn" />
<Button
android:text="ADD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addbtn" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/portfoliolist"
android:clickable="true" />
</LinearLayout>
</FrameLayout>
您的代码应为:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
var 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;
}
您可以参考此看看为什么您的fragment2
只在其顶部.我用ll.Visibility = ViewStates.Gone;
来避免它.
You can refer to this to see why your fragment2
just top on it. I use ll.Visibility = ViewStates.Gone;
to avoid it.
这篇关于Android-从另一个Fragment C#启动Fragemnt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!