首先第一步是:在布局
<FrameLayout android:id="@+id/fragelayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></FrameLayout> <RadioGroup android:id="@+id/radiogroup" android:gravity="center" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/wuye" android:button="@null" android:text="物业" android:textColor="@color/text_color" android:checked="true" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_margin="5dp" android:layout_height="wrap_content" android:drawableTop="@drawable/wuye" /> <RadioButton android:id="@+id/supmarts" android:button="@null" android:text="超市" android:textColor="@color/text_color" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_margin="5dp" android:layout_height="wrap_content" android:drawableTop="@drawable/supmar" /> <RadioButton android:id="@+id/tanchu" android:button="@null" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:background="@mipmap/open" /> <RadioButton android:id="@+id/sq" android:button="@null" android:text="商圈" android:textColor="@color/text_color" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_margin="5dp" android:layout_height="wrap_content" android:drawableTop="@drawable/sq" /> <RadioButton android:id="@+id/mine" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:button="@null" android:textColor="@color/text_color" android:drawableTop="@drawable/mine" android:gravity="center" android:text="我的" /> </RadioGroup>
第二步:在base里添加setparams
public abstract class BaseFragment extends Fragment { private Bundle params; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(getLayoutId(), container, false); initView(view); initData(); return view; } protected abstract int getLayoutId(); protected abstract void initView(View view); protected abstract void initData(); public Bundle getParams(){ return params; } public void setParams (Bundle params){ this.params=params; } }
public abstract class BaseActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getWindow().setStatusBarColor(0xff24cf5f); } setContentView(getLayout()); initView(); initDate(); } protected abstract int getLayout(); protected abstract void initView(); protected abstract void initDate(); }
第三步:在activity中
public class MainActivity extends BaseActivity { private RadioGroup radiogroup; private FragmentManager manager; public Map<String, Integer> fragments = new HashMap<>(); public Map<Integer, BaseFragment> lastFragments = new HashMap<>(); private BaseFragment lastFragment; private BaseFragment currentFragment; public void changFragment(int containerId, Class<? extends BaseFragment> fragmentClass, Bundle bundle, boolean isReplace, boolean isBack) { String fragmentName = fragmentClass.getSimpleName(); manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); currentFragment = (BaseFragment) manager.findFragmentByTag(fragmentName); if (currentFragment == null) { try { currentFragment = fragmentClass.newInstance(); transaction.add(containerId, currentFragment, fragmentName); fragments.put(fragmentName, containerId); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } if (isReplace) { if (lastFragments.size() > 0) { lastFragment = lastFragments.get(containerId); if (lastFragment != null) { fragments.remove(lastFragment); } } transaction.replace(containerId, currentFragment, fragmentName); } else { if (lastFragments.size() > 0) { lastFragment = lastFragments.get(containerId); if (lastFragment != null) { transaction.hide(lastFragment); } } transaction.show(currentFragment); } if (bundle != null) { currentFragment.setParams(bundle); } if (isBack) { transaction.addToBackStack(fragmentName); } transaction.commit(); lastFragments.put(containerId, currentFragment); } @Override protected void initDate() { radiogroup.check(R.id.supmarts); changFragment(R.id.fragelayout, SupmartFragment.class, null, false, false); radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.wuye: changFragment(R.id.fragelayout, WYFragment.class, null, false, false); break; case R.id.supmarts: changFragment(R.id.fragelayout, SupmartFragment.class, null, false, false); break; case R.id.tanchu: break; case R.id.sq: changFragment(R.id.fragelayout, SQFragment.class, null, false, false); break; case R.id.mine: changFragment(R.id.fragelayout, MineFragment1.class, null, false, false); break; } } }); } @Override protected int getLayout() { return R.layout.activity_main; } @Override protected void initView() { radiogroup = findViewById(R.id.radiogroup); } }