我有一个大小为10的arraylist,我想基于id加载一个片段,在四个不同的容器中加载4次。每次我使用循环向bundle中的片段发送不同的id,这是正确的。 但问题是我的片段只获得一个id(这是最后一个)并在布局上显示它。
我希望它能像这样工作, 当i = 0时:从arraylist获取id,将其传递给fragment并加载该id的数据并在container1中显示并对所有容器执行相同的操作。
请检查我的代码,我在加载或替换片段时犯了错误
SubFragment frag;
Bundle bundle;
RelativeLayout fLayout;
RelativeLayout.LayoutParams lp;
for (int i = 0; i<= categoryArrayList.size();i++ ) {
if (i != 6) {
if (i == 0){
frag = new SubFragment();
fLayout = new RelativeLayout(this);
bundle = new Bundle();
categoryArrayList.get(i).getChannelCategoryId();
bundle.putInt("categoryID", (categoryArrayList.get(i).getChannelCategoryId()));
bundle.putString("categoryName", categoryArrayList.get(i).getChannelName());
lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(50, 50, 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
fLayout.addView(, lp);
frag.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(fLayout.addView(fLayout,lp),frag)
.commit(); // how to replace a fragment here
}
else if(i==1){
frag = new SubFragment();
categoryArrayList.get(i).getChannelCategoryId();
bundle.putInt("categoryID", (categoryArrayList.get(i).getChannelCategoryId()));
bundle.putString("categoryName", categoryArrayList.get(i).getChannelName());
Log.v("for loop i",Integer.toString(i));
Log.v("for loop id",Integer.toString(categoryArrayList.get(i).getChannelCategoryId()));
Log.v("for loop name",categoryArrayList.get(i).getChannelName());
frag.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.container2, frag).commit();
}
else if(i==3){
frag = new SubFragment();
categoryArrayList.get(i).getChannelCategoryId();
bundle.putInt("categoryID", (categoryArrayList.get(i).getChannelCategoryId()));
bundle.putString("categoryName", categoryArrayList.get(i).getChannelName());
Log.v("for loop i",Integer.toString(i));
Log.v("for loop id",Integer.toString(categoryArrayList.get(i).getChannelCategoryId()));
Log.v("for loop name",categoryArrayList.get(i).getChannelName());
frag.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.container3, frag).commit();
}
}
else{
Log.v("else",Integer.toString(i));
break;
}
}
}