2014-05-02 44 views
0

我用這個來加載片段:更新數據再次

protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Fragment fragment = new Messaging(); 
     Bundle args = new Bundle(); 
     FriendInfo friend = null; 
     friend = friendAdapter.getItem(position); 
     args.putString(FriendInfo.USERNAME, friend.userName); 
     args.putString(FriendInfo.PORT, friend.port); 
     args.putString(FriendInfo.IP, friend.ip); 

     setTitle(friend.userName); 
     fragment.setArguments(args); 
     mTitle = getTitle(); 
     friendAdapter.notifyDataSetChanged(); 
     FragmentManager fragmentManager = getFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.frame_container, fragment).commit(); 
     mDrawerList.setItemChecked(position, true); 
     mDrawerList.setSelection(position); 
     mDrawerLayout.closeDrawer(mDrawerList); 

    } 

正如你可以看到這個加載相同的片段,每次,但我只需要有不同的值FriendInfo.USERNAMEFriendInfo.PORTFriendInfo.IP。 如何在不加載整個片段的情況下做到這一點?

回答

0

您可以使用FragmentManager的findFragmentById()findFragmentByTag()方法檢查片段是否已附加到活動。先試着找到片段 - 如果你找到它,只需自己更新;否則就像你已經做的那樣重新創建它。

+0

對不起,如果我已經表達不好,但我的實際問題是自己更新它。 – user3557747

+0

好吧,這是你的片段,你可以添加你想要的任何公共方法,這將做的伎倆。只需將從FragmentManager中找到的Fragment投射到適當的類型上即可。 – Karakuri

相關問題