2017-03-24 41 views
-2

我想添加片段,我設置了前一個背景#ffffff佈局當我去下一個片段時隱藏,但是在之前片段中的項目在下一個片段中工作時,片段不能隱藏原因?以前的片段不隱藏爲什麼?

這是我的代碼

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      if(isSearchHappened){ 
       android.support.v4.app.FragmentTransaction transection=getActivity().getSupportFragmentManager().beginTransaction(); 
       transection.hide(WorkOrderFragment.this); 
       DeliveryProcessFragment deliveryProcessFragment=new DeliveryProcessFragment(); 
       deliveryProcessFragment.jobBeans = searchArayList.get(position); 
       transection.add(R.id.frame, deliveryProcessFragment); 
       transection.commit(); 
      }else { 
       android.support.v4.app.FragmentTransaction transection=getActivity().getSupportFragmentManager().beginTransaction(); 
       transection.hide(WorkOrderFragment.this); 
       DeliveryProcessFragment deliveryProcessFragment=new DeliveryProcessFragment(); 
       deliveryProcessFragment.jobBeans = arrayList.get(position); 
       transection.add(R.id.frame, deliveryProcessFragment); 
       transection.commit(); 
      } 


     } 
    }); 
+0

'transection.add'不會從容器中刪除舊的片段。它增加了另一個之上的新的。要刪除以前,請使用'transection.replace' –

+0

當我使用transaction.replace和比以前再次回來的片段,數據不顯示 –

+0

這是另一個問題。 –

回答

0

這可能會幫助你。

FragmentManagerfragmentManager=getFragmentManager(); 
    FragmentTransactionfragmentTransaction=fragmentManager.beginTransaction(); 
    myFrag=newMyFrag(); 
    myFrag.setSpecialText("Fragtime:"+System.currentTimeMillis()); 
    myFragList.add(myFrag); 
    fragmentTransaction.add(R.id.myFrame,myFrag); 
    fragmentTransaction.commit(); 
} 
else { 
    if((myFragList.size()-1)>=0){ 
    FragmentManagerfragmentManager=getFragmentManager(); 
    FragmentTransactionfragmentTransaction=fragmentManager.beginTransaction(); 
    MyFraglastFrag=myFragList.get(myFragList.size()-1); 
    fragmentTransaction.remove(lastFrag); 
    myFragList.remove(lastFrag); 
    fragmentTransaction.commit(); 
    } 
}