package com.bawei.fragmentdemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.RadioGroup;
/**
* fragment加载方式
* 1.快捷菜单生成fragment,2,在布局中添加标签
*
*
* Fragment 常用方法
* 1.添加Fragment的方法:add()
* 2.替换Fragment的方法:replace()
* 3.移除Fragment的方法:remove()
* 4.隐藏/显示Fragment的方法:hide()/show()
*/
public class MainActivity extends AppCompatActivity {
private RadioGroup rg;
private FragmentA fragmentA;
private FragmentB fragmentB;
private FragmentC fragmentC;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg = findViewById(R.id.rg);
fragmentA = new FragmentA();
fragmentB = new FragmentB();
fragmentC = new FragmentC();
//获取管理器
FragmentManager fragmentManager = getSupportFragmentManager();
//开启管理器的事务
FragmentTransaction transaction = fragmentManager.beginTransaction();
//管理fragment
transaction.add(R.id.frame,fragmentA);
transaction.add(R.id.frame,fragmentB);
transaction.add(R.id.frame,fragmentC);
//默认显示fragmentA
transaction.show(fragmentA);
transaction.hide(fragmentB);
transaction.hide(fragmentC);
//transaction.replace(R.id.frame,fragmentA);//初始化
//提交
transaction.commit();
//单选框监听事件
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
//管理器服务
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (i){
case R.id.rb1:
//调用切换的方法
//changFragment(fragmentA);
//显示和隐藏
transaction.hide(fragmentB);
transaction.hide(fragmentC);
transaction.show(fragmentA);
transaction.commit();
break;
case R.id.rb2:
//调用切换的方法
//changFragment(fragmentB);
//显示和隐藏
transaction.hide(fragmentA);
transaction.hide(fragmentC);
transaction.show(fragmentB);
transaction.commit();
break;
case R.id.rb3:
//调用切换的方法
//changFragment(fragmentC);
//显示和隐藏
transaction.hide(fragmentB);
transaction.hide(fragmentA);
transaction.show(fragmentC);
transaction.commit();
break;
default:
break;
}
}
});
}
//切换Fragment方法
private void changFragment(Fragment fragment){
//开启管理器事务
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame,fragment);
transaction.commit();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"/>
<RadioGroup
android:id="@+id/rg"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RadioButton
android:id="@+id/rb1"
android:button="@null"
android:text="首页1"
android:drawableTop="@drawable/background"
android:textColor="@drawable/text_color"
android:checked="true"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textAlignment="center"/>
<RadioButton
android:id="@+id/rb2"
android:button="@null"
android:text="首页1"
android:drawableTop="@drawable/background"
android:textColor="@drawable/text_color"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textAlignment="center"/>
<RadioButton
android:id="@+id/rb3"
android:button="@null"
android:text="首页1"
android:drawableTop="@drawable/background"
android:textColor="@drawable/text_color"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textAlignment="center"/>
</RadioGroup>
</LinearLayout>