private BaseFragment lastFragment; private FragmentManager manager;public void addFragment(Class<? extends BaseFragment> fragment, int id) { manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); String simpleName = fragment.getSimpleName(); BaseFragment fragmentByTag = (BaseFragment) manager.findFragmentByTag(simpleName); if (fragmentByTag == null) { try { fragmentByTag = fragment.newInstance(); transaction.add(id, fragmentByTag, simpleName); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } if (lastFragment != null) { transaction.hide(lastFragment); } transaction.show(fragmentByTag); lastFragment = fragmentByTag; transaction.commit(); }
<RadioButton
android:layout_marginBottom="@dimen/dp_2"
android:background="@color/widget_color"
android:checked="true"
android:id="@+id/mHome"
android:layout_width="0dp"
android:layout_weight="1"
android:button="@null"
android:layout_height="match_parent"
android:drawableTop="@drawable/selecthome"
android:text="首页"
android:textSize="@dimen/dp_10"
android:layout_marginTop="@dimen/dp_10"
android:gravity="center"
/>