使用for循环添加片段 [英] Adding fragments using for loop

查看:51
本文介绍了使用for循环添加片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程Android也很陌生.我正在尝试使用for循环添加片段.可以说我想重复片段n次.

I am very new to programming an android as well. I am trying to add fragments using for loop. Lets say I would like to repeat fragment n-times.

        for (int i = 0; i < length; i++)
        {
        FragmentTransaction fragmentTransaction = this.FragmentManager.BeginTransaction();
        Fragment frag = _fragments[1];
        fragmentTransaction.Add(Resource.Id.frameForAddressFragment, frag);
        fragmentTransaction.AddToBackStack(null);
        fragmentTransaction.Commit();
        }

我想我必须用新名称为每个循环声明Fragment吗?我对吗?有人可以告诉我正确的方法吗?

I think I have to declare Fragment for every loop with new name? Am I right? Could some one show me right approach.

推荐答案

首先,我从不使用Xamarin,但是您不能对此稍作更改吗?我看不到为每个片段开始新事务的意义.提交也是如此.

First, I never use Xamarin, but can't you slightly change your code to this? I don't see the point of beginning a new transaction for each and every fragment. Same goes for committing.

    FragmentTransaction fragmentTransaction = this.FragmentManager.BeginTransaction();
    for (int i = 0; i < length; i++)
    {
    Fragment frag = _fragments[1];
    fragmentTransaction.Add(Resource.Id.frameForAddressFragment, frag);
    fragmentTransaction.AddToBackStack(null);
    }
    fragmentTransaction.Commit();

第二,我不知道这是否是一个错误,但是您编写了 Fragment frag = _fragments [1]; .我假设您的意思是 Fragment frag = _fragments [i];

Second, I don't know if it's a mistake, but you have written Fragment frag = _fragments[1];. I assume you meant Fragment frag = _fragments[i];

第三,您的代码有问题吗?提醒一下,您的"Resource.Id.frameForAddressFragment"应该是您要在其中添加片段的容器的ID.

Third, is there any problem with your code? As a reminder, your "Resource.Id.frameForAddressFragment" is supposed to be the ID of the container in which you want to add the fragment.

最后,这是什么意思?

我想我必须用新名称为每个循环声明Fragment吗?

I think I have to declare Fragment for every loop with new name?

您在说什么新名字"?

这篇关于使用for循环添加片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆