1.布局
<?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" android:orientation="vertical" tools:context="com.bwei.lenovo.day11_myapp.Activity.HomeActivity"> <FrameLayout android:id="@+id/home_frame" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></FrameLayout> <RadioGroup android:id="@+id/home_rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:background="@drawable/home_seletor1" android:id="@+id/home_rb1" android:layout_width="60dp" android:layout_height="60dp" android:layout_weight="1" android:button="@null" android:checked="true" /> <RadioButton android:background="@drawable/home_seletor2" android:id="@+id/home_rb2" android:layout_width="60dp" android:layout_height="60dp" android:layout_weight="1" android:button="@null" /> <RadioButton android:background="@drawable/home_seletor3" android:id="@+id/home_rb3" android:layout_width="60dp" android:layout_height="60dp" android:layout_weight="1" android:button="@null" /> <RadioButton android:background="@drawable/home_seletor4" android:id="@+id/home_rb4" android:layout_width="60dp" android:layout_height="60dp" android:layout_weight="1" android:button="@null" /> <RadioButton android:background="@drawable/home_seletor5" android:id="@+id/home_rb5" android:layout_width="60dp" android:layout_height="60dp" android:layout_weight="1" android:button="@null" /> </RadioGroup> </LinearLayout>下面开始写代码
package com.bwei.lenovo.day11_myapp.Activity; import android.support.annotation.IdRes; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RadioGroup; import com.bwei.lenovo.day11_myapp.R; import com.bwei.lenovo.day11_myapp.fragment.Home_fragment1; import com.bwei.lenovo.day11_myapp.fragment.Home_fragment2; import com.bwei.lenovo.day11_myapp.fragment.Home_fragment3; import com.bwei.lenovo.day11_myapp.fragment.Home_fragment4; import com.bwei.lenovo.day11_myapp.fragment.Home_fragment5; public class HomeActivity extends AppCompatActivity { private FragmentManager supportFragmentManager; private FragmentTransaction fragmentTransaction; private Home_fragment1 home_fragment1; private Home_fragment2 home_fragment2; private Home_fragment3 home_fragment3; private Home_fragment4 home_fragment4; private Home_fragment5 home_fragment5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); //获取id RadioGroup home_rg = (RadioGroup) findViewById(R.id.home_rg); //获取管理者 supportFragmentManager = getSupportFragmentManager(); //开启事务 fragmentTransaction = supportFragmentManager.beginTransaction(); //碎片 home_fragment1 = new Home_fragment1(); //提交事务 fragmentTransaction.add(R.id.home_frame, home_fragment1).commit(); home_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) { hideFrag(); FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); switch (checkedId) { case R.id.home_rb1: fragmentTransaction.show(home_fragment1).commit(); break; case R.id.home_rb2: if (home_fragment2 == null) { //实例化fragment2 home_fragment2 = new Home_fragment2(); fragmentTransaction.add(R.id.home_frame, home_fragment2).commit(); } else { //有的话就显示 fragmentTransaction.show(home_fragment2).commit(); } break; case R.id.home_rb3: if (home_fragment3 == null) { //实例化fragment3 home_fragment3 = new Home_fragment3(); fragmentTransaction.add(R.id.home_frame, home_fragment3).commit(); } else { //有的话就显示 fragmentTransaction.show(home_fragment3).commit(); } break; case R.id.home_rb4: if (home_fragment4 == null) { home_fragment4 = new Home_fragment4(); fragmentTransaction.add(R.id.home_frame, home_fragment4).commit(); } else { fragmentTransaction.show(home_fragment4).commit(); } break; case R.id.home_rb5: if (home_fragment5 == null) { home_fragment5 = new Home_fragment5(); fragmentTransaction.add(R.id.home_frame, home_fragment5).commit(); } else { fragmentTransaction.show(home_fragment5).commit(); } break; } } }); } private void hideFrag() { //再重新获取一个开启事务 FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); //不等于空或者是否添加的时候 if (home_fragment1 != null && home_fragment1.isAdded()) { fragmentTransaction.hide(home_fragment1); } if (home_fragment2 != null && home_fragment2.isAdded()) { fragmentTransaction.hide(home_fragment2); } if (home_fragment3 != null && home_fragment3.isAdded()) { fragmentTransaction.hide(home_fragment3); } if (home_fragment4 != null && home_fragment4.isAdded()) { fragmentTransaction.hide(home_fragment4); } if (home_fragment5 != null && home_fragment5.isAdded()) { fragmentTransaction.hide(home_fragment5); } fragmentTransaction.commit(); } }资源文件,决定图片的选中状态
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/f1" android:state_checked="true" /> <item android:drawable="@drawable/f"/> </selector>