用户主布局 Radiogroup+Fragment显示隐藏
packagecom.bwie.app.activity;
importandroid.support.annotation.IdRes;
importandroid.support.v4.app.FragmentManager;
importandroid.support.v4.app.FragmentTransaction;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.widget.BaseAdapter;
importandroid.widget.FrameLayout;
importandroid.widget.RadioButton;
importandroid.widget.RadioGroup;
importcom.bwie.app.R;
importcom.bwie.app.fragment.Fragment1;
importcom.bwie.app.fragment.Fragment2;
importcom.bwie.app.fragment.Fragment3;
importcom.bwie.app.fragment.Fragment4;
public class Index Activity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
private RadioGroupradioGroup;
//实例四个Fragment
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private Fragment 4fragment4;
//底部Radiogroup中的第一个radiobutton,初始化显示第一个,xml文件中b1默认选中
//Fragment管理器
private FragmentManagersupport FragmentManager;
@Override
public void ininView() {
supportFragmentManager=getSupportFragmentManager ();
radioGroup= (RadioGroup) findViewById(R.id.group);
b1= (RadioButton) findViewById(R.id.b1);
boolean checked =b1.isChecked();
//默认显示fragment1
if(checked==true){
FragmentTransaction fragmentTransaction1 =supportFragmentManager.beginTransaction();
if(fragment1==null){
fragment1=newFragment1();
fragmentTransaction1.add(R.id.frame,fragment1);
}else{
fragmentTransaction1.show(fragment1);
}
fragmentTransaction1.commit();
}
radioGroup.setOnCheckedChangeListener(this);
}
@Override
public int getView() {
return R.layout.activity_index;
}
@Override
public void onChecked Changed(RadioGroup group,@IdResintcheckedId) {
FragmentTransaction fragmentTransaction =supportFragmentManager.beginTransaction();
//隐藏所有的fragment
hideAllFragment(fragmentTransaction);
switch(checkedId){
caseR.id.b1:
FragmentTransaction fragmentTransaction1 =supportFragmentManager.beginTransaction();
if(fragment1==null){
fragment1=newFragment1();
fragmentTransaction1.add(R.id.frame,fragment1);
}else{
fragmentTransaction1.show(fragment1);
}
fragmentTransaction1.commit();
break;
caseR.id.b2:
FragmentTransaction fragmentTransaction2 =supportFragmentManager.beginTransaction();
if(fragment2==null){
fragment2=newFragment2();
fragmentTransaction2.add(R.id.frame,fragment2);
}else{
fragmentTransaction2.show(fragment2);
}
fragmentTransaction2.commit();
break;
caseR.id.b3:
FragmentTransaction fragmentTransaction3 =supportFragmentManager.beginTransaction();
if(fragment3==null){
fragment3=newFragment3();
fragmentTransaction3.add(R.id.frame,fragment3);
}else{
fragmentTransaction3.show(fragment3);
}
fragmentTransaction3.commit();
break;
caseR.id.b4:
FragmentTransaction fragmentTransaction4 =supportFragmentManager.beginTransaction();
if(fragment4==null){
fragment4=newFragment4();
fragmentTransaction4.add(R.id.frame,fragment4);
}else{
fragmentTransaction4.show(fragment4);
}
fragmentTransaction4.commit();
break;
}
}
//隐藏所有的Fragent
private voidhideAllFragment(FragmentTransaction fragmentTransaction) {
if(fragment1!=null){
fragmentTransaction.hide(fragment1);
}
if(fragment2!=null){
fragmentTransaction.hide(fragment2);
}
if(fragment3!=null){
fragmentTransaction.hide(fragment3);
}
if(fragment4!=null){
fragmentTransaction.hide(fragment4);
}
fragmentTransaction.commit();
}
}