安卓-从另一个片段中启动片段 C#[英] Android - Start Fragemnt from another Fragment C#

本文是小编为大家收集整理的关于安卓-从另一个片段中启动片段 C#的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我知道这个问题在这里被问到太多了,但是我尝试了所有解决方案以打开另一个片段的片段,没有人为我工作.

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> a <FrameLayout>来更改Fragment1的布局,但也没有工作.请帮助我找到解决方案.

编辑: 我已经在Fragment2中输入toast消息,当我单击fragment1中的add按钮时,toast消息(fragment2中的)显示这意味着启动正常,但布局没有在屏幕上显示.

推荐答案

您需要一个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;避免它.

其他推荐答案

您必须将Click事件附加到按钮 return view,我想您想将此事件连接到addbtn上您必须在附加此事件之前创建变量add

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 += delegate
    {
        Fragment2 fragment2 = new Fragment2();
        FragmentTransaction ft = FragmentManager.BeginTransaction();
        ft.Replace(Resource.Id.content_frame, fragment2);
        ft.Commit();
    };
    return view;
}

其他推荐答案

您可以改用这些事件:

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;
}

本文地址:https://www.itbaoku.cn/post/2091028.html

问题描述

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;
       }

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.

EDIT: 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.

推荐答案

You need a FrameLayout to contain your 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>

And your code should be :

    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;
    }

You can refer to this to see why your fragment2 just top on it. I use ll.Visibility = ViewStates.Gone; to avoid it.

其他推荐答案

You have to attach your Click event to your button before return 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

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 += delegate
    {
        Fragment2 fragment2 = new Fragment2();
        FragmentTransaction ft = FragmentManager.BeginTransaction();
        ft.Replace(Resource.Id.content_frame, fragment2);
        ft.Commit();
    };
    return view;
}

其他推荐答案

You can use the events instead:

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;
}