2016-11-12 53 views
0

我想在线性布局中多次添加片段。为此,我使用For循环并将数据传递给片段。每个索引都有不同的数据。我目前的问题是,我只能看到屏幕上的最后一个片段multipe时间可能是它隐藏顶部片段或任何其他原因。不明白是什么问题。以编程方式在线性布局中多次添加片段

 categoryArrayList = dbHelper.getChannelInfo(); 
     fragmentManager =getSupportFragmentManager(); 
     ft = fragmentManager.beginTransaction(); 
     for (int i = 0; i<categoryArrayList.size(); i++) { 

     SubFragment frag = new SubFragment(); 
      Bundle bundle = new Bundle(); 
     bundle.putInt("ID", (categoryArrayList.get(i).getChannelCategoryId())); 
     bundle.putString("categoryName", categoryArrayList.get(i).getChannelName()); 
      frag.setArguments(bundle); 
      ft.add(R.id.container, frag,"fragment"+i); 
    } 
    ft.commit(); 
} 
} 

XML:

<LinearLayout 
    android:id="@+id/container" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_marginTop="50dp" 
    android:background="#252424" 
    android:layout_below="@id/headercontainer" 
    android:layout_marginLeft="30dp"> 

回答

1

您使用的是每个Fragment的论点一样BundleFragmentTransaction小号同步发生,所以当他们最后执行时,Fragment s为全都阅读相同的论点。

你想必有类似的循环之前如下:

Bundle bundle = new Bundle(); 

将其移至for循环中,前put*()电话。

+0

现在一切都消失了,我在屏幕上看不到任何碎片。 – Andrain

+0

咦?这没有任何意义,如果你只是将该行移到'put *()'调用之前。你改变了什么? –

+0

我只是把你的发布代码投入到一个空的项目中,并做了一个简单的'SubFragment',它只是一个显示'categoryName'和'ID'参数的'TextView'。移动'Bundle'行所做的唯一事情就是纠正那些显示的参数。如果您完全确定这一切都是您改变的,那么您的代码中必须有其他内容出现在您的帖子中。请根据您现在的编码来编辑您的问题,并附上更多的代码。 –

相关问题