package com.example.administrator.lovegift;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;
import com.example.administrator.lovegift.fragment.CategoryFragment;
import com.example.administrator.lovegift.fragment.GuideFragment;
import com.example.administrator.lovegift.fragment.HortFragment;
import com.example.administrator.lovegift.fragment.MainFragment;
import java.lang.reflect.InvocationTargetException;
public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
private RadioGroup mRadioGroup;
private Fragment mCashFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
mRadioGroup.setOnCheckedChangeListener(this);
//首先默认加载第一个GuideFragment
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
mCashFragment = new GuideFragment();
ft.add(R.id.container_activity_main, mCashFragment, GuideFragment.TAG);
ft.commit();
}
/***********************
* RadioGroup.OnCheckedChangeListener的回调
******************************/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.controller_guide:
switchPage(GuideFragment.TAG, GuideFragment.class);
break;
case R.id.controller_hort:
switchPage(HortFragment.TAG, HortFragment.class);
break;
case R.id.controller_category:
switchPage(CategoryFragment.TAG, CategoryFragment.class);
break;
case R.id.controller_main:
switchPage(MainFragment.TAG, MainFragment.class);
break;
}
}
//切换Fragment 的方法
private void switchPage(String TAG, Class<? extends Fragment> cls) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
//隐藏当前显示的页面
if (mCashFragment != null) {
tr.hide(mCashFragment);
}
//去FragmentManager栈中查找将要显示的页面是否存在
mCashFragment = fm.findFragmentByTag(TAG);
if (mCashFragment != null) {
//显示页面
tr.show(mCashFragment);
} else {
try {
mCashFragment = cls.getConstructor().newInstance();
tr.add(R.id.container_activity_main, mCashFragment, TAG);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
//提交
tr.commit();
}
}
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.mygift.MainActivity">
<FrameLayout
android:id="@+id/container_activity_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<include
layout="@layout/activity_controller"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/activity_controller_height"
android:background="#F5F5F5"
>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
>
<RadioButton
android:id="@+id/controller_guide"
style="@style/below_activity"
android:text="指南"
android:drawableTop="@drawable/guide"
android:checked="true"
/>
<RadioButton
android:id="@+id/controller_hort"
style="@style/below_activity"
android:text="热门"
android:drawableTop="@drawable/hort"
/>
<RadioButton
android:id="@+id/controller_category"
style="@style/below_activity"
android:text="分类"
android:drawableTop="@drawable/category"
/>
<RadioButton
android:id="@+id/controller_main"
style="@style/below_activity"
android:text="我的"
android:drawableTop="@drawable/main"
/>
</RadioGroup>
</LinearLayout>