Fragment与RadioButton结合使用

//初始化控件

[html]  view plain  copy
  1. private void initView() {  
  2.     rb_1 = (RadioButton) findViewById(R.id.rb_01);  
  3.     rb_2 = (RadioButton) findViewById(R.id.rb_02);  
  4.    rb_3 = (RadioButton) findViewById(R.id.rb_03);  
  5.    rb_4 = (RadioButton) findViewById(R.id.rb_04);  


//初始化监听事件

[html]  view plain  copy
  1. private void initEvents() {  
  2.     rb_1.setOnClickListener(this);  
  3.     rb_2.setOnClickListener(this);  
  4.     rb_3.setOnClickListener(this);  
  5.     rb_4.setOnClickListener(this);  
  6. }  

//重写监听方法

[html]  view plain  copy
  1. @Override  
  2. public void onClick(View v) {  
  3.   
  4.     switch (v.getId()) {  
  5.         case R.id.rb_01:  
  6.             select(0);  
  7.             break;  
  8.         case R.id.rb_02:  
  9.             select(1);  
  10.             break;  
  11.         case R.id.rb_03:  
  12.             select(2);  
  13.             break;  
  14.         case R.id.rb_04:  
  15.             select(3);  
  16.             break;  
  17.     }  
  18.   
  19.   
  20. }  

//管理Fragment的方法,在监听事件中调用

[html]  view plain  copy
  1. private void select(int i) {  
  2.        FragmentManager fm = getSupportFragmentManager();  //获得Fragment管理器  
  3.        FragmentTransaction ft = fm.beginTransaction(); //开启一个事务  
  4.   
  5.        hidtFragment(ft);   //隐藏Fragment的方法,先判断fragment是否为空,如果不为空则隐藏Fragment  
  6.   
  7.        switch (i) {//点击切换fragment,如果fragment为空,则创建,如果不为空,就显示  
  8.            case 0:  
  9.                if (firstFragment == null) {  
  10.                    firstFragment = new Fragment1();  
  11.                    ft.add(R.id.FramLaout, firstFragment);  
  12.                } else {  
  13.                    ft.show(firstFragment);  
  14.                }  
  15.                break;  
  16.            case 1:  
  17.                if (secondFragment == null) {  
  18.                    secondFragment = new Fragment2();  
  19.                    ft.add(R.id.FramLaout, secondFragment);  
  20.                } else {  
  21.                    ft.show(secondFragment);  
  22.                }  
  23.                break;  
  24.            case 2:  
  25.                if (thirdFragment == null) {  
  26.                    thirdFragment = new Fragment3();  
  27.                    ft.add(R.id.FramLaout, thirdFragment);  
  28.                } else {  
  29.                    ft.show(thirdFragment);  
  30.                }  
  31.                break;  
  32.            case 3:  
  33.                if (fourFragment == null) {  
  34.                    fourFragment = new Fragment4();  
  35.                    ft.add(R.id.FramLaout, fourFragment);  
  36.                } else {  
  37.                    ft.show(fourFragment);  
  38.                }  
  39.                break;  
  40.        }  
  41.        ft.commit();   //提交事务  
  42.   
  43.    }  

//隐藏所有的Fragment,在上面会用到

[html]  view plain  copy
  1. private void hidtFragment(FragmentTransaction fragmentTransaction) {  
  2.        if (firstFragment != null) {  
  3.            fragmentTransaction.hide(firstFragment);  
  4.   
  5.        }  
  6.        if (secondFragment != null) {  
  7.            fragmentTransaction.hide(secondFragment);  
  8.   
  9.        }  
  10.        if (thirdFragment != null) {  
  11.            fragmentTransaction.hide(thirdFragment);  
  12.   
  13.        }  
  14.        if (fourFragment != null) {  
  15.            fragmentTransaction.hide(fourFragment);  
  16.   
  17.        }  
  18.   
  19.    }  

//设置第一个页面为默认页面
 
 
[html] view plain copy
  1. private void initDefultView() {  
  2.     FragmentManager fm = getSupportFragmentManager();  
  3.     FragmentTransaction ft = fm.beginTransaction();  
  4.     firstFragment = new Fragment1();  
  5.     ft.add(R.id.FramLaout, firstFragment);  
  6.     ft.commit();  
  7. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值