public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Fragment fm;
private Home_Fragment fragment;
private Class_Fragment fragment2;
private Shopping_Fragment fragment3;
private Myebuy_Fragment fragment4;
private RadioButton rd1;
private RadioButton rd2;
private RadioButton rd3;
private RadioButton rd4;
private FragmentManager manager;
private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//判断home_pager是否为null
if (fragment == null) {
fragment = new Home_Fragment();
//设置首页默认被选中
rd1.setChecked(true);
}
changeFragment(fragment);
}
private void initView() {
rd1 = (RadioButton) findViewById(R.id.b1);
rd2 = (RadioButton) findViewById(R.id.b2);
rd3 = (RadioButton) findViewById(R.id.b3);
rd4 = (RadioButton) findViewById(R.id.b4);
rd1.setOnClickListener(this);
rd2.setOnClickListener(this);
rd3.setOnClickListener(this);
rd4.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.b1:
{
if (fragment==null){
fragment=new Home_Fragment();
}
changeFragment(fragment);
}
break;
case R.id.b2:
{
if (fragment2==null){
fragment2=new Class_Fragment();
}
changeFragment(fragment2);
}
break;
case R.id.b3:
{
if (fragment3==null){
fragment3=new Shopping_Fragment();
}
changeFragment(fragment3);
}
break;
case R.id.b4:
{
if (fragment4==null){
fragment4=new Myebuy_Fragment();
}
changeFragment(fragment4);
}
break;
}
}
//切换fragment
public void changeFragment(Fragment ment){
manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
if (fm !=null){
transaction.hide(fm);
}
// isAdded:是否被添加过 被添加过 is true 反之
if (!ment.isAdded()) {
// 调用replace 添加fragment
transaction.add(R.id.fl, ment);
} else {
// 显示出来
transaction.show(ment);
}
// 提交
transaction.commit();
// 记录当前的Fragment
fm = ment;
}
}
private Fragment fm;
private Home_Fragment fragment;
private Class_Fragment fragment2;
private Shopping_Fragment fragment3;
private Myebuy_Fragment fragment4;
private RadioButton rd1;
private RadioButton rd2;
private RadioButton rd3;
private RadioButton rd4;
private FragmentManager manager;
private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//判断home_pager是否为null
if (fragment == null) {
fragment = new Home_Fragment();
//设置首页默认被选中
rd1.setChecked(true);
}
changeFragment(fragment);
}
private void initView() {
rd1 = (RadioButton) findViewById(R.id.b1);
rd2 = (RadioButton) findViewById(R.id.b2);
rd3 = (RadioButton) findViewById(R.id.b3);
rd4 = (RadioButton) findViewById(R.id.b4);
rd1.setOnClickListener(this);
rd2.setOnClickListener(this);
rd3.setOnClickListener(this);
rd4.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.b1:
{
if (fragment==null){
fragment=new Home_Fragment();
}
changeFragment(fragment);
}
break;
case R.id.b2:
{
if (fragment2==null){
fragment2=new Class_Fragment();
}
changeFragment(fragment2);
}
break;
case R.id.b3:
{
if (fragment3==null){
fragment3=new Shopping_Fragment();
}
changeFragment(fragment3);
}
break;
case R.id.b4:
{
if (fragment4==null){
fragment4=new Myebuy_Fragment();
}
changeFragment(fragment4);
}
break;
}
}
//切换fragment
public void changeFragment(Fragment ment){
manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
if (fm !=null){
transaction.hide(fm);
}
// isAdded:是否被添加过 被添加过 is true 反之
if (!ment.isAdded()) {
// 调用replace 添加fragment
transaction.add(R.id.fl, ment);
} else {
// 显示出来
transaction.show(ment);
}
// 提交
transaction.commit();
// 记录当前的Fragment
fm = ment;
}
}