1. 选择1
displayFrag 要显示的fragment
private void display(Fragment displayFrag) { if (fragment != null) { ft.hide(fragment); } if (fragment != displayFrag || fragment==null ) { fm = getSupportFragmentManager(); ft = fm.beginTransaction(); if (displayFrag == null) return; if (displayFrag.isAdded()) { ft.show(displayFrag); } else { ft.add(R.id.container, displayFrag); } fragment = displayFrag; ft.commit(); } }
2.选择2
@Bind(R.id.fl_containers)
FrameLayout flContainers;
@Bind(R.id.rb_home)
RadioButton rbHome;
@Bind(R.id.rb_find)
RadioButton rbFind;
@Bind(R.id.rb_message)
RadioButton rbMessage;
@Bind(R.id.rb_shopping)
RadioButton rbShopping;
@Bind(R.id.rb_my)
RadioButton rbMy;
@Bind(R.id.rg_buttons)
RadioGroup rgButtons;
private FragmentManager fm;
FragmentTransaction ft;
Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
rgButtons = (RadioGroup) findViewById(R.id.rg_buttons);
rbHome = (RadioButton) findViewById(R.id.rb_home);
select(R.id.rb_home);
rbFind = (RadioButton) findViewById(R.id.rb_find);
rbMessage = (RadioButton) findViewById(R.id.rb_message);
rbShopping = (RadioButton) findViewById(R.id.rb_shopping);
rbMy = (RadioButton) findViewById(R.id.rb_my);
rgButtons.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.rb_home:
select(R.id.rb_home);
break;
case R.id.rb_find:
select(R.id.rb_find);
break;
case R.id.rb_message:
select(R.id.rb_message);
break;
case R.id.rb_shopping:
select(R.id.rb_shopping);
break;
case R.id.rb_my:
select(R.id.rb_my);
break;
}
}
});
}
private void select(int i){
{
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
switch (i) {
case R.id.rb_home:
rbHome.setChecked(true);
hideFragment();
fragment= fm.findFragmentByTag("fragmentHome");
if(fragment==null){
fragment=new FragmentHome();
ft.add(R.id.fl_containers,fragment,"fragmentHome");
}else{
ft.show(fragment);
}
break;
case R.id.rb_find:
rbFind.setChecked(true);
hideFragment();
fragment= fm.findFragmentByTag("fragmentFind");
if(fragment==null){
fragment=new FragmentFind();
ft.add(R.id.fl_containers,fragment,"fragmentFind");
}else{
ft.show(fragment);
}
break;
case R.id.rb_message:
rbMessage.setChecked(true);
hideFragment();
fragment = fm.findFragmentByTag("FragmentMessage");
if(fragment==null){
fragment= new FragmentMessage();
ft.add(R.id.fl_containers,fragment,"FragmentMessage");
}else{
ft.show(fragment);
}
break;
case R.id.rb_shopping:
rbShopping.setChecked(true);
hideFragment();
fragment = fm.findFragmentByTag("fragmentShopping");
if(fragment==null){
fragment = new FragmentShopping();
ft.add( R.id.fl_containers,fragment,"fragmentShopping");
}else{
ft.show(fragment);
}
break;
case R.id.rb_my:
rbMy.setChecked(true);
hideFragment();
fragment = fm.findFragmentByTag("fragmentMy");
if(fragment==null){
fragment = new FragmentMy();
ft.add(R.id.fl_containers,fragment,"fragmentMy");
}else{
ft.show(fragment);
}
break;
}
ft.commit();
}
}
private void hideFragment() {
if(fragment!=null){
ft.hide(fragment);
}
}
}