2016-03-14 107 views
0

当我单击按钮时,应该如何将相同的片段重复显示为相同的布局?当我尝试在线性布局下重复使用相同的片段时,它只会添加第一个片段而不是其他片段。单击按钮时,将相同的片段重复为相同的布局

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

您应该检查片段是否为空。如果不是,则重新使用它:

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); 
} 
相关问题