2014-04-30 41 views
0

在導航抽屜中,我有一個帶有列表視圖的子片段。通過第一次點擊抽屜項目,將顯示列表視圖,但是當通過單擊相同抽屜項目來關閉並重新打開抽屜時,子片段中的列表視圖被更新,即新子列表項目被添加到子片段的列表視圖中列表視圖的先前值..please help..i我堅持導航抽屜項目單擊一次又一次更新兒童片段

int select=0; 
select=position-2; 
     String sendto =list.get(position-2).getIMEI(); 
     String sendtoname=list.get(position-2).getName(); 
     System.out.println(deviceIMEI+"--"+sendto+sendtoname); 
    Fragment fragment = new ChatFrag(); 
    Bundle args1 = new Bundle(); 
    args1.putString(ChatFrag.MYIEMI, deviceIMEI); 
    args1.putString(ChatFrag.SENDERIEMI, sendto); 
    args1.putString(ChatFrag.SENDERNAME,sendtoname); 
    args1.putInt(ChatFrag.SELECT, select); 
    fragment.setArguments(args1); 
    fm.beginTransaction() 
    .replace(R.id.content_frame, fragment) 
    .commit(); 

這是code..please幫助如何記住指數..

回答

0

的原因是DrawerListViewonItemClick會當列表中的項目被點擊時調用。這是ListView的行爲。

解決此問題的一種方法是始終記住last selected index並避免在下次if the selected index is same as last selected時再次添加Fragment

另一種方法是檢查FragmentManager如果Fragment已添加

List<Fragment> fragmentList = getSupportFragmentManager().getFragments(); 

    if(fragmentList != null) { 
     for (Fragment fragment : fragmentList) { 
      if (fragment != null && fragment instanceof CustomFragment) { 
       // don't add the fragment again 
      }else{ 
      // add the fragment here 
     } 
    } 
+0

我已經更新了我的question..have看看那個 – Aakash