Fragment添加和隐藏

package com.example.android_1505d_fragment_day10_dome02;

import com.example.android_1505D_day010_dome02.fragment.Fragment01;
import com.example.android_1505D_day010_dome02.fragment.Fragment02;
import com.example.android_1505D_day010_dome02.fragment.Fragment03;
import com.example.android_1505d_fragment_day10_dome01.R;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
/**
 *
 *1.静态切换Fragment
 *2.findFragmentById获取fragment实例
 * @author lenovo
 *
 */

public class MainActivity extends FragmentActivity {

    private RadioGroup radioGroup;
    private Fragment01 fragment01;
    private Fragment02 fragment02;
    private Fragment03 fragment03;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //获取资源ID
       radioGroup=(RadioGroup) findViewById(R.id.rg);
       
       fragment01=(Fragment01) getSupportFragmentManager().findFragmentById(R.id.fragment1);
       fragment02=(Fragment02) getSupportFragmentManager().findFragmentById(R.id.fragment2);
       fragment03=(Fragment03) getSupportFragmentManager().findFragmentById(R.id.fragment3);
       
   
       //对radioGroup添加监听 来控制fragment显隐
       radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            
            switch (checkedId) {
            case R.id.r_but1:
                //显示第一个fragment隐藏第二个 第3个
                getSupportFragmentManager().beginTransaction().show(fragment01).hide(fragment02).hide(fragment03).commit();
                
                break;
            case R.id.r_but2:
                //显示第2个fragment隐藏第1个 第3个
                getSupportFragmentManager().beginTransaction().show(fragment02).hide(fragment01).hide(fragment03).commit();
                        
                break;
            case R.id.r_but3:
                //显示第3个fragment隐藏第二个 第1个
                getSupportFragmentManager().beginTransaction().show(fragment03).hide(fragment02).hide(fragment01).commit();
                
                break;

            default:
                break;
            }
            
            
        }
    });
       
       
    
    }

    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值