单击按钮时,如何将相同的片段重复到相同的布局?当我尝试在线性布局中重复相同的片段时,它只会添加第一个而不是其他片段。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_4);
problemsLayout = (LinearLayout)findViewById(R.id.problemsLayout);
FragmentManager fragmentManager =getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for(int i =0 ; i<OperationProblemSelectTask.operationProblemList.size();i++){
addProblemView(fragmentTransaction,OperationProblemSelectTask.operationProblemList.get(i).getMahine_problem(),OperationProblemSelectTask.operationProblemList.get(i).getMachine());
Log.i("OperationProblemSelectTaskObjects",""+OperationProblemSelectTask.operationProblemList.get(i).getMahine_problem());
}
fragmentTransaction.commit();
(MachineProblemFragment)getSupportFragmentManager().findFragmentById(R.id.fragment3);
}
public void addProblemView(FragmentTransaction fragmentTransaction , String problem , String machine){
problemViewFragment = new ProblemViewFragment(problem, machine);
fragmentTransaction.add(R.id.problemsLayout, problemViewFragment, "problemView");
}
答案 0 :(得分:0)
您应该检查片段是否为空。如果不是,那么重复使用它:
private void addXTOneQuickStartBoosterFragment(int animationType, boolean shouldShow) {
String fragmentTag = getFragmentTag();
FragmentTransaction fragmentTransaction = getFragmentTransactionWithAnimation(animationType);
mXTOneQuickStartFragment = (SupplementXTOneQuickStartFragment) getFragmentManager().findFragmentByTag(fragmentTag);
if(mXTOneQuickStartFragment == null) {
mXTOneQuickStartFragment = new SupplementXTOneQuickStartFragment();
Bundle xTOneQuickStartBundle = getSupplementBundle(false);
xTOneQuickStartBundle.putBoolean(SupplementXTOneQuickStartFragment.IS_ENGLISH, mIsEnglish);
mXTOneQuickStartFragment.setArguments(xTOneQuickStartBundle);
}
finishFragmentAnimation(fragmentTransaction, mXTOneQuickStartFragment, fragmentTag);
}