0
我有執行片段和我的應用底杆問題..與底杆開關片段(顯示/隱藏)的Android
我灣切換根據片段初始化底杆.. 此我的代碼
private Fragment frag_summary = new SummaryFragment();
private Fragment frag_my_proposal = new MyProposalFragment();
private Fragment frag_proposal = new ProposalFragment();
private Fragment frag_friends = new FriendsFragment();
bottomNavigation.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes int tabId) {
setSelectedFragment(tabId);
}
});
public void setSelectedFragment(int id) {
switch (id) {
case R.id.menu_home:
setFragment(id, frag_summary);
break;
case R.id.menu_my_proposals:
setFragment(id, new MyProposalFragment());
break;
case R.id.menu_proposal:
setFragment(id, new ProposalFragment());
break;
case R.id.menu_friends:
setFragment(id, new FriendsFragment());
break;
case R.id.menu_more:
/* drawer.openDrawer(GravityCompat.START);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}*/
break;
default:
setFragment(id, frag_summary);
break;
}
}
private void setFragment(int id, Fragment fragments) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
String fragmentTag = String.valueOf(id);
if (frag_summary.isAdded()) {
fragmentTransaction.hide(frag_summary);
}
if (frag_my_proposal.isAdded()) {
fragmentTransaction.hide(frag_my_proposal);
}
if (frag_proposal.isAdded()) {
fragmentTransaction.hide(frag_proposal);
}
if (frag_friends.isAdded()) {
fragmentTransaction.hide(frag_friends);
}
if (fragments.isAdded()) {
fragmentTransaction.show(fragments);
} else {
fragmentTransaction.add(R.id.content_frame, fragments, fragmentTag);
}
fragmentTransaction.commitAllowingStateLoss();
}
問題是:當我從片段frag_summary選擇片段frag_friends時,片段frag_summary和其他片段有時仍然顯示..像其他活動ex:從片段frag_friends到片段frag_proposal。
sho如何解決它? 對不起我的英語
感謝,但我想片段同樣沒有重裝切換。我想切換到最後一個片段的條件... –
我給出的建議也可以。但是真正的問題是,在調用'setFragment'時不是傳遞實例變量,而是發送每個片段的新實例。我編輯了我的答案 – Dibzmania