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;
}
}
});
}
}
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;
}
}
});
}
}