public class MainActivity extends FragmentActivity {
private FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
final Fragment1 one = new Fragment1();
final Fragment2 two = new Fragment2();
final Fragment3 three = new Fragment3();
final Fragment4 four = new Fragment4();
transaction.add(R.id.frag, one);
transaction.add(R.id.frag, two);
transaction.add(R.id.frag, three);
transaction.add(R.id.frag, four);
// 隐藏和显示
transaction.hide(one);
transaction.hide(two);
transaction.hide(three);
transaction.show(four);
// 提交(s事物提交后 就不能再用了 如果想再用 就得重新开启)
transaction.commit();
// 点击事件 分类这些按钮的时候会触发这个事件
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
FragmentTransaction transaction2 = manager.beginTransaction();
switch (checkedId) {
case R.id.radio0:
//隐藏
transaction2.hide(two);
transaction2.hide(three);
transaction2.hide(four);
//展示
transaction2.show(one);
break;
case R.id.radio1:
//隐藏
transaction2.hide(one);
transaction2.hide(three);
transaction2.hide(four);
//展示
transaction2.show(two);
break;
case R.id.radio2:
//隐藏
transaction2.hide(two);
transaction2.hide(one);
transaction2.hide(four);
//展示
transaction2.show(three);
break;
case R.id.radio3:
//隐藏
transaction2.hide(two);
transaction2.hide(three);
transaction2.hide(one);
//展示
transaction2.show(four);
break;
default:
break;
}
transaction2.commit();
}
});
}
}
activity_main.xml 布局文件中
<FrameLayout
android:id="@+id/frag"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/sel_message"
android:gravity="center"
android:padding="5dp"
android:text="首页" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:drawableTop="@drawable/sel_see"
android:padding="5dp"
android:text="分类" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/sel_start"
android:gravity="center"
android:padding="5dp"
android:text="购物车" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/sel_people"
android:gravity="center"
android:padding="5dp"
android:text="我的" />
</RadioGroup>
frag.xml 代码
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaaa" />