【发布时间】:2018-06-02 21:40:18
【问题描述】:
我知道这个问题在这里被问得太多,但我已经尝试了所有解决方案来打开另一个片段中的片段,但没有人为我工作。
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 的布局,但也没有成功。请帮助我找到解决方案。
编辑:
我在Fragment2 中添加了toast 消息,当我单击fragment1 中的add 按钮时,toast 消息(位于fragment2 中)正在显示,这意味着@987654334 @ 开始正常,但它的布局没有显示在屏幕上。
【问题讨论】:
-
您试图在方法返回后附加一个侦听器,这永远不会发生。
-
您好,我的回答对您有用吗?
-
@JoeLv-MSFT 是的,但你能告诉我如何在按钮单击时从片段中高亮
NavuagtionDrawer项目吗?因为现在当我单击按钮时,它会将我带到第二个片段,但当我滑动 Navigationdrawer 菜单时,第一个片段仍然是突出显示的片段。
标签: c# android android-fragments xamarin fragment