public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private ArrayList<Fragment> arrayList;
private RadioGroup rg;
private RadioButton[] radioButtons;
private FragmentManager manager;
private int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
arrayList = new ArrayList<>();
NewsFragment fragment1 = new NewsFragment();
AppraisalFragment fragment2 = new AppraisalFragment();
PictureFragment fragment3 = new PictureFragment();
CommentFragment fragment4 = new CommentFragment();
arrayList.add(fragment1);
arrayList.add(fragment2);
arrayList.add(fragment3);
arrayList.add(fragment4);
radioButtons=new RadioButton[rg.getChildCount()];
for (int i = 0; i < radioButtons.length; i++) {
radioButtons[i]= (RadioButton) rg.getChildAt(i);
}
manager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=manager.beginTransaction();
fragmentTransaction.add(R.id.main_layout,arrayList.get(0));
fragmentTransaction.commit();
radioButtons[0].setChecked(true);
rg.setOnCheckedChangeListener(this);
}
private void initView() {
rg = (RadioGroup) findViewById(R.id.rg);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction transaction=manager.beginTransaction();
for (int i = 0; i < radioButtons.length; i++) {
if (radioButtons[i].getId()==checkedId){
if (arrayList.get(i).isAdded()){
transaction.show(arrayList.get(i)).hide(arrayList.get(count)).commit();
}else {
transaction.add(R.id.main_layout,arrayList.get(i)).hide(arrayList.get(count)).commit();
}
count=i;
}
}
}
}
//布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.douyu.main.MainActivity">
<FrameLayout
android:layout_weight="1"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<RadioGroup
android:id="@+id/rg"
android:background="@android:color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="56dp">
<RadioButton
android:textColor="@color/textcolor"
android:gravity="center"
android:text="首页"
android:layout_marginTop="2dp"
android:button="@null"
android:drawableTop="@drawable/homepage_item"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:textColor="@color/textcolor"
android:gravity="center"
android:text="分类"
android:layout_marginTop="2dp"
android:drawableTop="@drawable/classify_item"
android:button="@null"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:textColor="@color/textcolor"
android:gravity="center"
android:text="关注"
android:layout_marginTop="2dp"
android:drawableTop="@drawable/attention_item"
android:button="@null"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:textColor="@color/textcolor"
android:gravity="center"
android:text="鱼吧"
android:layout_marginTop="2dp"
android:drawableTop="@drawable/bar_item"
android:button="@null"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:textColor="@color/textcolor"
android:gravity="center"
android:text="我的"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:drawableTop="@drawable/user_item"
android:button="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
//text文字颜色,在res下创建color文件夹
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/black" android:state_checked="false"/>
<item android:color="#ff6600" android:state_checked="true"/>
</selector>