案例一:
MainActivity:
package com.xiaoyehai.switchfragment;
import android.support.v4.app.Fragment;
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.FrameLayout;
import android.widget.RadioGroup;
import com.xiaoyehai.switchfragment.base.BaseFragment;
import com.xiaoyehai.switchfragment.fragment.CommonFrameFragment;
import com.xiaoyehai.switchfragment.fragment.CustomFragment;
import com.xiaoyehai.switchfragment.fragment.OtherFragment;
import com.xiaoyehai.switchfragment.fragment.ThirdPartyFragment;
import java.util.ArrayList;
import java.util.List;
/**
* Fragment切换
*/
public class MainActivity extends AppCompatActivity {
private FrameLayout mFlContent;
private RadioGroup mRadioGroup;
private List<BaseFragment> list;
private int position;
private Fragment nextFragment; //上一个显示的fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initFragment();
initListener();
}
private void initListener() {
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_common_frame:
position = 0;
break;
case R.id.rb_thirdparty:
position = 1;
break;
case R.id.rb_custom:
position = 2;
break;
case R.id.rb_other:
position = 3;
break;
default:
position = 0;
break;
}
//根据位置得到对应的fragment
BaseFragment currentFragment = getFragment();
//切换fragment
// switchFragmentByReplace(currentFragment);
switchFragmentByAdd(nextFragment, currentFragment);
}
});
mRadioGroup.check(R.id.rb_common_frame);
}
/**
* add()方法切换fragment
*
* @param from 上一个fragment
* @param to 当前要显示的fragment
*/
private void switchFragmentByAdd(Fragment from, Fragment to) {
if (from != to) { //不是同一个才切换
nextFragment = to;
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (!to.isAdded()) { //当前要显示的Fragment没有被添加
if (from != null) {
transaction.hide(from);
}
if (to != null) {
transaction.add(R.id.fl_content, to).commit();
}
} else { //当前要显示的Fragment已经被添加
if (from != null) {
transaction.hide(from);
}
if (to != null) {
transaction.show(to).commit();
}
}
}
}
/**
* replace()方法切换fragment
*
* @param fragment
*/
private void switchFragmentByReplace(BaseFragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fl_content, fragment);
transaction.commit();
}
/**
* 根据位置得到对应的fragment
*
* @return
*/
private BaseFragment getFragment() {
BaseFragment fragment = list.get(position);
return fragment;
}
private void initFragment() {
list = new ArrayList<>();
list.add(new CommonFrameFragment());
list.add(new ThirdPartyFragment());
list.add(new CustomFragment());
list.add(new OtherFragment());
}
private void initViews() {
mFlContent = (FrameLayout) findViewById(R.id.fl_content);
mRadioGroup = (RadioGroup) findViewById(R.id.rg_bottom_tag);
}
}
布局文件:
<?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">
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</FrameLayout>
<RadioGroup
android:id="@+id/rg_bottom_tag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#11000000"
android:orientation="horizontal"
android:padding="3dp">
<RadioButton
android:id="@+id/rb_common_frame"
style="@style/bottom_tag_style"
android:drawableTop="@drawable/rb_common_frame_drawable_selector"
android:text="常用框架"/>
<RadioButton
android:id="@+id/rb_thirdparty"
style="@style/bottom_tag_style"
android:drawableTop="@drawable/rb_thirdparty_drawable_selector"
android:text="第三方"/>
<RadioButton
android:id="@+id/rb_custom"
style="@style/bottom_tag_style"
android:drawableTop="@drawable/rb_custom_drawable_selector"
android:text="自定义控件"/>
<RadioButton
android:id="@+id/rb_other"
style="@style/bottom_tag_style"
android:drawableTop="@drawable/rb_other_drawable_selector"
android:text="其他"/>
</RadioGroup>
</LinearLayout>
样式:
<style name="bottom_tag_style">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item>
<item name="android:textSize">14sp</item>
<item name="android:gravity">center</item>
<item name="android:drawablePadding">3dp</item>
<item name="android:textColor">@drawable/rb_textcolor_selector</item>
<item name="android:layout_weight">1</item>
</style>
颜色选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#363636" android:state_checked="false"/>
<item android:color="#3097FD" android:state_checked="true"/>
</selector>
图片选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_tab_video_press" android:state_checked="true"/>
<item android:drawable="@drawable/ic_tab_video"/>
</selector>
BaseFragment:
package com.xiaoyehai.switchfragment.base;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Fragment的基类
* Created by xiaoyehai on 2017/6/10.
*/
public abstract class BaseFragment extends Fragment {
/**
* 上下文对象
*/
public Context mContext;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return initView();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
}
/**
* 加载数据,由子类实现
*/
protected void initData() {
}
/**
* 初始化布局,由子类实现
*
* @return
*/
protected abstract View initView();
}
Fragment:
package com.xiaoyehai.switchfragment.fragment;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import com.xiaoyehai.switchfragment.base.BaseFragment;
/**
* 常用框架Fragment
* Created by xiaoyehai on 2017/6/10.
*/
public class CommonFrameFragment extends BaseFragment {
private static final String TAG = CommonFrameFragment.class.getSimpleName();
private TextView mTextView;
@Override
protected View initView() {
Log.e(TAG, "CommonFrameFragment页面被初始化了");
mTextView = new TextView(mContext);
mTextView.setTextSize(20);
mTextView.setTextColor(Color.RED);
mTextView.setGravity(Gravity.CENTER);
return mTextView;
}
@Override
protected void initData() {
super.initData();
Log.e(TAG, "CommonFrameFragment数据被初始化了");
mTextView.setText("常用框架页面");
}
}
切换方法二:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
//设置默认Fragment
mTransaction = getSupportFragmentManager().beginTransaction();
CommonFrameFragment commonFrameFragment = new CommonFrameFragment();
mTransaction.replace(R.id.fl_content, commonFrameFragment, "commonFrameFragment").commit();
mRadioGroup.check(R.id.rb_common_frame);
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
mTransaction = getSupportFragmentManager().beginTransaction();
switch (checkedId) {
case R.id.rb_common_frame:
CommonFrameFragment commonFrameFragment = new CommonFrameFragment();
mTransaction.replace(R.id.fl_content, commonFrameFragment, "commonFrameFragment");
break;
case R.id.rb_thirdparty:
ThirdPartyFragment thirdPartyFragment = new ThirdPartyFragment();
mTransaction.replace(R.id.fl_content, thirdPartyFragment, "thirdPartyFragment");
break;
case R.id.rb_custom:
CustomFragment customFragment = new CustomFragment();
mTransaction.replace(R.id.fl_content, customFragment, "customFragment");
break;
case R.id.rb_other:
OtherFragment otherFragment = new OtherFragment();
mTransaction.replace(R.id.fl_content, otherFragment, "otherFragment");
break;
}
mTransaction.commit();
}
});
}
切换方法三:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
//设置默认Fragment
mTransaction = getSupportFragmentManager().beginTransaction();
if (mCommonFrameFragment == null) {
mCommonFrameFragment = new CommonFrameFragment();
mTransaction.add(R.id.fl_content, mCommonFrameFragment, "commonFrameFragment");
}
hideFragment(mTransaction);
mTransaction.show(mCommonFrameFragment);
mTransaction.commit();
mRadioGroup.check(R.id.rb_common_frame);
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
mTransaction = getSupportFragmentManager().beginTransaction();
switch (checkedId) {
case R.id.rb_common_frame:
if (mCommonFrameFragment == null) {
mCommonFrameFragment = new CommonFrameFragment();
mTransaction.add(R.id.fl_content, mCommonFrameFragment, "commonFrameFragment");
}
hideFragment(mTransaction);
mTransaction.show(mCommonFrameFragment);
break;
case R.id.rb_thirdparty:
if (mThirdPartyFragment == null) {
mThirdPartyFragment = new ThirdPartyFragment();
mTransaction.add(R.id.fl_content, mThirdPartyFragment, "thirdPartyFragment");
}
hideFragment(mTransaction);
mTransaction.show(mThirdPartyFragment);
break;
case R.id.rb_custom:
if (mCustomFragment == null) {
mCustomFragment = new CustomFragment();
mTransaction.add(R.id.fl_content, mCustomFragment, "customFragment");
}
hideFragment(mTransaction);
mTransaction.show(mCustomFragment);
break;
case R.id.rb_other:
if (mOtherFragment == null) {
mOtherFragment = new OtherFragment();
mTransaction.add(R.id.fl_content, mOtherFragment, "otherFragment");
}
hideFragment(mTransaction);
mTransaction.show(mOtherFragment);
break;
}
mTransaction.commit();
}
});
}
/**
* 隐藏所有的fragment
*
* @param transaction
*/
private void hideFragment(FragmentTransaction transaction) {
if (mCommonFrameFragment != null) {
transaction.hide(mCommonFrameFragment);
}
if (mThirdPartyFragment != null) {
transaction.hide(mThirdPartyFragment);
}
if (mCustomFragment != null) {
transaction.hide(mCustomFragment);
}
if (mOtherFragment != null) {
transaction.hide(mOtherFragment);
}
}
不同级别Fragment之间的切换:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFragment();
}
private void initFragment() {
mFragment1 = new Fragment1();
mFragment2 = new Fragment2();
mFragment3 = new Fragment3();
//设置默认fragment
addFragment(mFragment1, AppConstant.FRAGMENT1_TAG);
}
private void addFragment(Fragment fragment, String tag) {
getSupportFragmentManager().beginTransaction().add(R.id.flayout_content, fragment, tag).commit();
}
/**
* 切换fragment
*
* @param tagFrom
* @param tagTo
*/
public void switchFragment(String tagFrom, String tagTo) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment from = fragmentManager.findFragmentByTag(tagFrom);
Fragment to = fragmentManager.findFragmentByTag(tagTo);
transaction.hide(from);
if (to == null) {
addFragment(getFragment(tagTo), tagTo);
} else {
transaction.show(to);
}
transaction.commitAllowingStateLoss();
}
private Fragment getFragment(String tag) {
Fragment fragment = null;
switch (tag) {
case AppConstant.FRAGMENT2_TAG:
fragment = mFragment2;
break;
case AppConstant.FRAGMENT3_TAG:
fragment = mFragment3;
break;
}
return fragment;
}
在Fragment中调用:
mMainActivity.switchFragment(AppConstant.FRAGMENT1_TAG, AppConstant.FRAGMENT2_TAG);
通用:
mFragmentManager = getSupportFragmentManager();
mFragment1 = new Fragment1();
mFragment2 = new Fragment2();
mFragment3 = new Fragment3();
mFragment4 = new Fragment4();
FragmentTransaction transaction = mFragmentManager.beginTransaction();
transaction.add(R.id.fl_content, mFragment1, Constant.FRAGMENT1_TAG).commit();
previousFragmentTag = Constant.FRAGMENT1_TAG;
mRadioGroup.check(R.id.rb_1);
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_1:
switchFragment(previousFragmentTag, Constant.FRAGMENT1_TAG);
break;
case R.id.rb_2:
switchFragment(previousFragmentTag, Constant.FRAGMENT2_TAG);
break;
case R.id.rb_3:
switchFragment(previousFragmentTag, Constant.FRAGMENT3_TAG);
break;
}
}
});
}
public void switchFragment(String fromTag, String toTag) {
FragmentTransaction transaction = mFragmentManager.beginTransaction();
Fragment from = getFragment(fromTag);
if (from != null) {
transaction.hide(from);
}
Fragment to = getFragment(toTag);
if (to.isAdded()) {
transaction.show(to);
} else {
transaction.add(R.id.fl_content, to, toTag);
}
transaction.commit();
previousFragmentTag = toTag;
}
public Fragment getFragment(String tag) {
Fragment fragment = null;
switch (tag) {
case Constant.FRAGMENT1_TAG:
fragment = mFragment1;
break;
case Constant.FRAGMENT2_TAG:
fragment = mFragment2;
break;
case Constant.FRAGMENT3_TAG:
fragment = mFragment3;
break;
case Constant.FRAGMENT4_TAG:
fragment = mFragment4;
break;
}
return fragment;
}