简体   繁体   中英

Multiple instances of the same fragment?

I am making an Android app with Visual Studio and C#. I know I should porbably use Java, but I prefer C#, so I chose to use that. But anyway, I have a problem where I can't add the same fragment twice. I know this is a duplicate, but none of the other questions helped at all. Here is one I looked at: Adding multiple instances of the same fragment

All of my code is pretty much the same, except only one fragment gets added. Here is my code:

protected override void OnCreate(Bundle bundle) {
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    var trans = FragmentManager.BeginTransaction();

    for(int i = 0; i < 5; i++)
        trans.Add(Resource.Id.bottomLayout, new BottomFragment(), "Fragment_" + i.ToString());

    trans.Commit();
}

Resource.Id.bottomLayout is a vertical LinearLayout, so I have no idea what the problem is. I feel like everyone will just get all mad because it's a duplicate(because that always happens to me, which is why I use this site as a last resort), but if I could receive some help, it would be appreciated.

I would assume that all five fragments are directly on top of each other.

Apply an android:orientation="vertical" or android:orientation="vertical" to your LinearLayout in order to get them to expand the LinearLayout :

Vertical:

<LinearLayout
  android:id="@+id/bottomLayout"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" />

Horizontal:

<LinearLayout
  android:id="@+id/bottomLayout"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:orientation="horizontal" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM