UI框架Radiogroup+Fragment顯隱

用戶主布局 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();

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容