我知道这个问题在这里问了太多,但是我已经尝试过每个解决方案从另一个片段打开一个片段,没有人为我工作。
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
按钮不会发生任何事情。这是为什么 ?我试图改变Fragment1
的布局,而不是<LinearLayout>
一个<FrameLayout>
,但也没有用。请帮我找一个解决方案。
修改
我在toast
中添加了Fragment2
消息,当我点击add
中的fragment1
按钮时,toast
消息(位于fragment2
中1}})显示这意味着fragment2
开始正常,但它的布局没有显示在屏幕上。
答案 0 :(得分:0)
您必须在<{strong> .../Index?Keyword=someText&IsResYearChecked=true&IsResYearChecked=false&IsResNumChecked=false&IsResTextChecked=true&IsResTextChecked=false
Click
之前将您的return
事件附加到按钮,我认为您希望将此事件附加到{ {1}}因此您必须先创建变量view
才能附加此事件
addbtn
答案 1 :(得分:0)
您可以改为使用这些事件:
public override 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;
}
答案 2 :(得分:0)
您需要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;
}
您可以参考this了解您的fragment2
为何只是最重要的原因。我使用ll.Visibility = ViewStates.Gone;
来避免它。